consumer_avformat.c: clean up the dual pass log at the end of the second pass.
[melted] / src / modules / avformat / consumer_avformat.c
1 /*
2 * consumer_avformat.c -- an encoder based on avformat
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_consumer.h>
24 #include <framework/mlt_frame.h>
25
26 // System header files
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <pthread.h>
32 #include <sys/time.h>
33 #include <math.h>
34 #include <unistd.h>
35
36 // avformat header files
37 #include <avformat.h>
38 #ifdef SWSCALE
39 #include <swscale.h>
40 #endif
41 #include <opt.h>
42
43 //
44 // This structure should be extended and made globally available in mlt
45 //
46
47 typedef struct
48 {
49 int16_t *buffer;
50 int size;
51 int used;
52 double time;
53 int frequency;
54 int channels;
55 }
56 *sample_fifo, sample_fifo_s;
57
58 sample_fifo sample_fifo_init( int frequency, int channels )
59 {
60 sample_fifo this = calloc( 1, sizeof( sample_fifo_s ) );
61 this->frequency = frequency;
62 this->channels = channels;
63 return this;
64 }
65
66 // sample_fifo_clear and check are temporarily aborted (not working as intended)
67
68 void sample_fifo_clear( sample_fifo this, double time )
69 {
70 int words = ( float )( time - this->time ) * this->frequency * this->channels;
71 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) && this->used > words && words > 0 )
72 {
73 memmove( this->buffer, &this->buffer[ words ], ( this->used - words ) * sizeof( int16_t ) );
74 this->used -= words;
75 this->time = time;
76 }
77 else if ( ( int )( ( float )time * 100 ) != ( int )( ( float )this->time * 100 ) )
78 {
79 this->used = 0;
80 this->time = time;
81 }
82 }
83
84 void sample_fifo_check( sample_fifo this, double time )
85 {
86 if ( this->used == 0 )
87 {
88 if ( ( int )( ( float )time * 100 ) < ( int )( ( float )this->time * 100 ) )
89 this->time = time;
90 }
91 }
92
93 void sample_fifo_append( sample_fifo this, int16_t *samples, int count )
94 {
95 if ( ( this->size - this->used ) < count )
96 {
97 this->size += count * 5;
98 this->buffer = realloc( this->buffer, this->size * sizeof( int16_t ) );
99 }
100
101 memcpy( &this->buffer[ this->used ], samples, count * sizeof( int16_t ) );
102 this->used += count;
103 }
104
105 int sample_fifo_used( sample_fifo this )
106 {
107 return this->used;
108 }
109
110 int sample_fifo_fetch( sample_fifo this, int16_t *samples, int count )
111 {
112 if ( count > this->used )
113 count = this->used;
114
115 memcpy( samples, this->buffer, count * sizeof( int16_t ) );
116 this->used -= count;
117 memmove( this->buffer, &this->buffer[ count ], this->used * sizeof( int16_t ) );
118
119 this->time += ( double )count / this->channels / this->frequency;
120
121 return count;
122 }
123
124 void sample_fifo_close( sample_fifo this )
125 {
126 free( this->buffer );
127 free( this );
128 }
129
130 // Forward references.
131 static int consumer_start( mlt_consumer this );
132 static int consumer_stop( mlt_consumer this );
133 static int consumer_is_stopped( mlt_consumer this );
134 static void *consumer_thread( void *arg );
135 static void consumer_close( mlt_consumer this );
136
137 /** Initialise the dv consumer.
138 */
139
140 mlt_consumer consumer_avformat_init( mlt_profile profile, char *arg )
141 {
142 // Allocate the consumer
143 mlt_consumer this = mlt_consumer_new( profile );
144
145 // If memory allocated and initialises without error
146 if ( this != NULL )
147 {
148 // Get properties from the consumer
149 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
150
151 // Assign close callback
152 this->close = consumer_close;
153
154 // Interpret the argument
155 if ( arg != NULL )
156 mlt_properties_set( properties, "target", arg );
157
158 // sample and frame queue
159 mlt_properties_set_data( properties, "frame_queue", mlt_deque_init( ), 0, ( mlt_destructor )mlt_deque_close, NULL );
160
161 // Audio options not fully handled by AVOptions
162 #define QSCALE_NONE (-99999)
163 mlt_properties_set_int( properties, "aq", QSCALE_NONE );
164
165 // Video options not fully handled by AVOptions
166 mlt_properties_set_int( properties, "dc", 8 );
167
168 // Muxer options not fully handled by AVOptions
169 mlt_properties_set_double( properties, "muxdelay", 0.7 );
170 mlt_properties_set_double( properties, "muxpreload", 0.5 );
171
172 // Ensure termination at end of the stream
173 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
174
175 // Default to separate processing threads for producer and consumer with no frame dropping!
176 mlt_properties_set_int( properties, "real_time", -1 );
177
178 // Set up start/stop/terminated callbacks
179 this->start = consumer_start;
180 this->stop = consumer_stop;
181 this->is_stopped = consumer_is_stopped;
182 }
183
184 // Return this
185 return this;
186 }
187
188 /** Start the consumer.
189 */
190
191 static int consumer_start( mlt_consumer this )
192 {
193 // Get the properties
194 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
195
196 // Check that we're not already running
197 if ( !mlt_properties_get_int( properties, "running" ) )
198 {
199 // Allocate a thread
200 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
201
202 // Get the width and height
203 int width = mlt_properties_get_int( properties, "width" );
204 int height = mlt_properties_get_int( properties, "height" );
205
206 // Obtain the size property
207 char *size = mlt_properties_get( properties, "s" );
208
209 // Interpret it
210 if ( size != NULL )
211 {
212 int tw, th;
213 if ( sscanf( size, "%dx%d", &tw, &th ) == 2 && tw > 0 && th > 0 )
214 {
215 width = tw;
216 height = th;
217 }
218 else
219 {
220 fprintf( stderr, "%s: Invalid size property %s - ignoring.\n", __FILE__, size );
221 }
222 }
223
224 // Now ensure we honour the multiple of two requested by libavformat
225 width = ( width / 2 ) * 2;
226 height = ( height / 2 ) * 2;
227 mlt_properties_set_int( properties, "width", width );
228 mlt_properties_set_int( properties, "height", height );
229
230 // We need to set these on the profile as well because the s property is
231 // an alias to mlt properties that correspond to profile settings.
232 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
233 if ( profile )
234 {
235 profile->width = width;
236 profile->height = height;
237 }
238
239 // Handle the ffmpeg command line "-r" property for frame rate
240 if ( mlt_properties_get( properties, "r" ) )
241 {
242 double frame_rate = mlt_properties_get_double( properties, "r" );
243 AVRational rational = av_d2q( frame_rate, 255 );
244 mlt_properties_set_int( properties, "frame_rate_num", rational.num );
245 mlt_properties_set_int( properties, "frame_rate_den", rational.den );
246 if ( profile )
247 {
248 profile->frame_rate_num = rational.num;
249 profile->frame_rate_den = rational.den;
250 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
251 }
252 }
253
254 // Apply AVOptions that are synonyms for standard mlt_consumer options
255 if ( mlt_properties_get( properties, "ac" ) )
256 mlt_properties_set_int( properties, "channels", mlt_properties_get_int( properties, "ac" ) );
257 if ( mlt_properties_get( properties, "ar" ) )
258 mlt_properties_set_int( properties, "frequency", mlt_properties_get_int( properties, "ar" ) );
259
260 // Assign the thread to properties
261 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
262
263 // Set the running state
264 mlt_properties_set_int( properties, "running", 1 );
265
266 // Create the thread
267 pthread_create( thread, NULL, consumer_thread, this );
268 }
269 return 0;
270 }
271
272 /** Stop the consumer.
273 */
274
275 static int consumer_stop( mlt_consumer this )
276 {
277 // Get the properties
278 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
279
280 // Check that we're running
281 if ( mlt_properties_get_int( properties, "running" ) )
282 {
283 // Get the thread
284 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
285
286 // Stop the thread
287 mlt_properties_set_int( properties, "running", 0 );
288
289 // Wait for termination
290 pthread_join( *thread, NULL );
291 }
292
293 return 0;
294 }
295
296 /** Determine if the consumer is stopped.
297 */
298
299 static int consumer_is_stopped( mlt_consumer this )
300 {
301 // Get the properties
302 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
303 return !mlt_properties_get_int( properties, "running" );
304 }
305
306 /** Process properties as AVOptions and apply to AV context obj
307 */
308
309 static void apply_properties( void *obj, mlt_properties properties, int flags )
310 {
311 int i;
312 int count = mlt_properties_count( properties );
313 for ( i = 0; i < count; i++ )
314 {
315 const char *opt_name = mlt_properties_get_name( properties, i );
316 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
317 if ( opt != NULL )
318 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
319 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
320 #else
321 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
322 #endif
323 }
324 }
325
326 /** Add an audio output stream
327 */
328
329 static AVStream *add_audio_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
330 {
331 // Get the properties
332 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
333
334 // Create a new stream
335 AVStream *st = av_new_stream( oc, 1 );
336
337 // If created, then initialise from properties
338 if ( st != NULL )
339 {
340 AVCodecContext *c = st->codec;
341
342 // Establish defaults from AVOptions
343 avcodec_get_context_defaults2( c, CODEC_TYPE_AUDIO );
344
345 c->codec_id = codec_id;
346 c->codec_type = CODEC_TYPE_AUDIO;
347
348 // Setup multi-threading
349 int thread_count = mlt_properties_get_int( properties, "threads" );
350 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
351 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
352 if ( thread_count > 1 )
353 avcodec_thread_init( c, thread_count );
354
355 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
356 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
357
358 // Allow the user to override the audio fourcc
359 if ( mlt_properties_get( properties, "atag" ) )
360 {
361 char *tail = NULL;
362 char *arg = mlt_properties_get( properties, "atag" );
363 int tag = strtol( arg, &tail, 0);
364 if( !tail || *tail )
365 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
366 c->codec_tag = tag;
367 }
368
369 // Process properties as AVOptions
370 apply_properties( c, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
371
372 int audio_qscale = mlt_properties_get_int( properties, "aq" );
373 if ( audio_qscale > QSCALE_NONE )
374 {
375 c->flags |= CODEC_FLAG_QSCALE;
376 c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
377 }
378
379 // Set parameters controlled by MLT
380 c->sample_rate = mlt_properties_get_int( properties, "frequency" );
381 c->channels = mlt_properties_get_int( properties, "channels" );
382
383 if ( mlt_properties_get( properties, "alang" ) != NULL )
384 strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
385 }
386 else
387 {
388 fprintf( stderr, "%s: Could not allocate a stream for audio\n", __FILE__ );
389 }
390
391 return st;
392 }
393
394 static int open_audio( AVFormatContext *oc, AVStream *st, int audio_outbuf_size )
395 {
396 // We will return the audio input size from here
397 int audio_input_frame_size = 0;
398
399 // Get the context
400 AVCodecContext *c = st->codec;
401
402 // Find the encoder
403 AVCodec *codec = avcodec_find_encoder( c->codec_id );
404
405 // Continue if codec found and we can open it
406 if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
407 {
408 // ugly hack for PCM codecs (will be removed ASAP with new PCM
409 // support to compute the input frame size in samples
410 if ( c->frame_size <= 1 )
411 {
412 audio_input_frame_size = audio_outbuf_size / c->channels;
413 switch(st->codec->codec_id)
414 {
415 case CODEC_ID_PCM_S16LE:
416 case CODEC_ID_PCM_S16BE:
417 case CODEC_ID_PCM_U16LE:
418 case CODEC_ID_PCM_U16BE:
419 audio_input_frame_size >>= 1;
420 break;
421 default:
422 break;
423 }
424 }
425 else
426 {
427 audio_input_frame_size = c->frame_size;
428 }
429
430 // Some formats want stream headers to be seperate (hmm)
431 if( !strcmp( oc->oformat->name, "mp4" ) ||
432 !strcmp( oc->oformat->name, "mov" ) ||
433 !strcmp( oc->oformat->name, "3gp" ) )
434 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
435 }
436 else
437 {
438 fprintf( stderr, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
439 }
440
441 return audio_input_frame_size;
442 }
443
444 static void close_audio( AVFormatContext *oc, AVStream *st )
445 {
446 avcodec_close( st->codec );
447 }
448
449 /** Add a video output stream
450 */
451
452 static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int codec_id )
453 {
454 // Get the properties
455 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
456
457 // Create a new stream
458 AVStream *st = av_new_stream( oc, 0 );
459
460 if ( st != NULL )
461 {
462 char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
463 AVCodecContext *c = st->codec;
464
465 // Establish defaults from AVOptions
466 avcodec_get_context_defaults2( c, CODEC_TYPE_VIDEO );
467
468 c->codec_id = codec_id;
469 c->codec_type = CODEC_TYPE_VIDEO;
470
471 // Setup multi-threading
472 int thread_count = mlt_properties_get_int( properties, "threads" );
473 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
474 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
475 if ( thread_count > 1 )
476 avcodec_thread_init( c, thread_count );
477
478 // Process properties as AVOptions
479 apply_properties( c, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM );
480
481 // Set options controlled by MLT
482 c->width = mlt_properties_get_int( properties, "width" );
483 c->height = mlt_properties_get_int( properties, "height" );
484 c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
485 c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
486 st->time_base = c->time_base;
487 c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
488
489 if ( codec_id == CODEC_ID_DVVIDEO )
490 {
491 // Compensate for FFmpeg's notion of DV aspect ratios, which are
492 // based upon a width of 704. Since we do not have a normaliser
493 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
494 // we just coerce the values to facilitate a passive behaviour through
495 // the rescale normaliser when using equivalent producers and consumers.
496 // = display_aspect / (width * height)
497 double ar = mlt_properties_get_double( properties, "aspect_ratio" );
498 if ( ar == 8.0/9.0 ) // 4:3 NTSC
499 {
500 c->sample_aspect_ratio.num = 10;
501 c->sample_aspect_ratio.den = 11;
502 }
503 else if ( ar == 16.0/15.0 ) // 4:3 PAL
504 {
505 c->sample_aspect_ratio.num = 159;
506 c->sample_aspect_ratio.den = 54;
507 }
508 else if ( ar == 32.0/27.0 ) // 16:9 NTSC
509 {
510 c->sample_aspect_ratio.num = 40;
511 c->sample_aspect_ratio.den = 33;
512 }
513 else // 16:9 PAL
514 {
515 c->sample_aspect_ratio.num = 118;
516 c->sample_aspect_ratio.den = 81;
517 }
518 }
519 else if ( mlt_properties_get( properties, "aspect" ) )
520 {
521 // "-aspect" on ffmpeg command line is display aspect ratio
522 double ar = mlt_properties_get_double( properties, "aspect" );
523 AVRational rational = av_d2q( ar, 255 );
524
525 // Update the profile and properties as well since this is an alias
526 // for mlt properties that correspond to profile settings
527 mlt_properties_set_int( properties, "display_aspect_num", rational.num );
528 mlt_properties_set_int( properties, "display_aspect_den", rational.den );
529 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
530 if ( profile )
531 {
532 profile->display_aspect_num = rational.num;
533 profile->display_aspect_den = rational.den;
534 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
535 }
536
537 // Now compute the sample aspect ratio
538 rational = av_d2q( ar * c->height / c->width, 255 );
539 c->sample_aspect_ratio = rational;
540 // Update the profile and properties as well since this is an alias
541 // for mlt properties that correspond to profile settings
542 mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
543 mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
544 if ( profile )
545 {
546 profile->sample_aspect_num = rational.num;
547 profile->sample_aspect_den = rational.den;
548 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
549 }
550
551 }
552 else
553 {
554 c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
555 c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
556 }
557 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
558 st->sample_aspect_ratio = c->sample_aspect_ratio;
559 #endif
560
561 if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
562 {
563 c->flags |= CODEC_FLAG_QSCALE;
564 st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
565 }
566
567 // Allow the user to override the video fourcc
568 if ( mlt_properties_get( properties, "vtag" ) )
569 {
570 char *tail = NULL;
571 const char *arg = mlt_properties_get( properties, "vtag" );
572 int tag = strtol( arg, &tail, 0);
573 if( !tail || *tail )
574 tag = arg[ 0 ] + ( arg[ 1 ] << 8 ) + ( arg[ 2 ] << 16 ) + ( arg[ 3 ] << 24 );
575 c->codec_tag = tag;
576 }
577
578 // Some formats want stream headers to be seperate
579 if ( oc->oformat->flags & AVFMT_GLOBALHEADER )
580 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
581
582 // Translate these standard mlt consumer properties to ffmpeg
583 if ( mlt_properties_get_int( properties, "progressive" ) == 0 &&
584 mlt_properties_get_int( properties, "deinterlace" ) == 0 )
585 {
586 if ( mlt_properties_get_int( properties, "ildct" ) )
587 c->flags |= CODEC_FLAG_INTERLACED_DCT;
588 if ( mlt_properties_get_int( properties, "ilme" ) )
589 c->flags |= CODEC_FLAG_INTERLACED_ME;
590 }
591
592 // parse the ratecontrol override string
593 int i;
594 char *rc_override = mlt_properties_get( properties, "rc_override" );
595 for ( i = 0; rc_override; i++ )
596 {
597 int start, end, q;
598 int e = sscanf( rc_override, "%d,%d,%d", &start, &end, &q );
599 if ( e != 3 )
600 fprintf( stderr, "%s: Error parsing rc_override\n", __FILE__ );
601 c->rc_override = av_realloc( c->rc_override, sizeof( RcOverride ) * ( i + 1 ) );
602 c->rc_override[i].start_frame = start;
603 c->rc_override[i].end_frame = end;
604 if ( q > 0 )
605 {
606 c->rc_override[i].qscale = q;
607 c->rc_override[i].quality_factor = 1.0;
608 }
609 else
610 {
611 c->rc_override[i].qscale = 0;
612 c->rc_override[i].quality_factor = -q / 100.0;
613 }
614 rc_override = strchr( rc_override, '/' );
615 if ( rc_override )
616 rc_override++;
617 }
618 c->rc_override_count = i;
619 if ( !c->rc_initial_buffer_occupancy )
620 c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
621 c->intra_dc_precision = mlt_properties_get_int( properties, "dc" ) - 8;
622
623 // Setup dual-pass
624 i = mlt_properties_get_int( properties, "pass" );
625 if ( i == 1 )
626 c->flags |= CODEC_FLAG_PASS1;
627 else if ( i == 2 )
628 c->flags |= CODEC_FLAG_PASS2;
629 if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
630 {
631 char logfilename[1024];
632 FILE *f;
633 int size;
634 char *logbuffer;
635
636 snprintf( logfilename, sizeof(logfilename), "%s_2pass.log",
637 mlt_properties_get( properties, "passlogfile" ) ? mlt_properties_get( properties, "passlogfile" ) : mlt_properties_get( properties, "target" ) );
638 if ( c->flags & CODEC_FLAG_PASS1 )
639 {
640 f = fopen( logfilename, "w" );
641 if ( !f )
642 perror( logfilename );
643 else
644 mlt_properties_set_data( properties, "_logfile", f, 0, ( mlt_destructor )fclose, NULL );
645 }
646 else
647 {
648 /* read the log file */
649 f = fopen( logfilename, "r" );
650 if ( !f )
651 {
652 perror(logfilename);
653 }
654 else
655 {
656 mlt_properties_set( properties, "_logfilename", logfilename );
657 fseek( f, 0, SEEK_END );
658 size = ftell( f );
659 fseek( f, 0, SEEK_SET );
660 logbuffer = av_malloc( size + 1 );
661 if ( !logbuffer )
662 fprintf( stderr, "%s: Could not allocate log buffer\n", __FILE__ );
663 else
664 {
665 size = fread( logbuffer, 1, size, f );
666 fclose( f );
667 logbuffer[size] = '\0';
668 c->stats_in = logbuffer;
669 mlt_properties_set_data( properties, "_logbuffer", logbuffer, 0, ( mlt_destructor )av_free, NULL );
670 }
671 }
672 }
673 }
674 }
675 else
676 {
677 fprintf( stderr, "%s: Could not allocate a stream for video\n", __FILE__ );
678 }
679
680 return st;
681 }
682
683 static AVFrame *alloc_picture( int pix_fmt, int width, int height )
684 {
685 // Allocate a frame
686 AVFrame *picture = avcodec_alloc_frame();
687
688 // Determine size of the
689 int size = avpicture_get_size(pix_fmt, width, height);
690
691 // Allocate the picture buf
692 uint8_t *picture_buf = av_malloc(size);
693
694 // If we have both, then fill the image
695 if ( picture != NULL && picture_buf != NULL )
696 {
697 // Fill the frame with the allocated buffer
698 avpicture_fill( (AVPicture *)picture, picture_buf, pix_fmt, width, height);
699 }
700 else
701 {
702 // Something failed - clean up what we can
703 av_free( picture );
704 av_free( picture_buf );
705 picture = NULL;
706 }
707
708 return picture;
709 }
710
711 static int open_video(AVFormatContext *oc, AVStream *st)
712 {
713 // Get the codec
714 AVCodecContext *video_enc = st->codec;
715
716 // find the video encoder
717 AVCodec *codec = avcodec_find_encoder( video_enc->codec_id );
718
719 if( codec && codec->pix_fmts )
720 {
721 const enum PixelFormat *p = codec->pix_fmts;
722 for( ; *p!=-1; p++ )
723 {
724 if( *p == video_enc->pix_fmt )
725 break;
726 }
727 if( *p == -1 )
728 video_enc->pix_fmt = codec->pix_fmts[ 0 ];
729 }
730
731 // Open the codec safely
732 return codec != NULL && avcodec_open( video_enc, codec ) >= 0;
733 }
734
735 void close_video(AVFormatContext *oc, AVStream *st)
736 {
737 avcodec_close(st->codec);
738 }
739
740 static inline long time_difference( struct timeval *time1 )
741 {
742 struct timeval time2;
743 gettimeofday( &time2, NULL );
744 return time2.tv_sec * 1000000 + time2.tv_usec - time1->tv_sec * 1000000 - time1->tv_usec;
745 }
746
747 /** The main thread - the argument is simply the consumer.
748 */
749
750 static void *consumer_thread( void *arg )
751 {
752 // Map the argument to the object
753 mlt_consumer this = arg;
754
755 // Get the properties
756 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
757
758 // Get the terminate on pause property
759 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
760 int terminated = 0;
761
762 // Determine if feed is slow (for realtime stuff)
763 int real_time_output = mlt_properties_get_int( properties, "real_time" );
764
765 // Time structures
766 struct timeval ante;
767
768 // Get the frame rate
769 double fps = mlt_properties_get_double( properties, "fps" );
770
771 // Get width and height
772 int width = mlt_properties_get_int( properties, "width" );
773 int height = mlt_properties_get_int( properties, "height" );
774 int img_width = width;
775 int img_height = height;
776
777 // Get default audio properties
778 mlt_audio_format aud_fmt = mlt_audio_pcm;
779 int channels = mlt_properties_get_int( properties, "channels" );
780 int frequency = mlt_properties_get_int( properties, "frequency" );
781 int16_t *pcm = NULL;
782 int samples = 0;
783
784 // AVFormat audio buffer and frame size
785 int audio_outbuf_size = 10000;
786 uint8_t *audio_outbuf = av_malloc( audio_outbuf_size );
787 int audio_input_frame_size = 0;
788
789 // AVFormat video buffer and frame count
790 int frame_count = 0;
791 int video_outbuf_size = ( 1024 * 1024 );
792 uint8_t *video_outbuf = av_malloc( video_outbuf_size );
793
794 // Used for the frame properties
795 mlt_frame frame = NULL;
796 mlt_properties frame_properties = NULL;
797
798 // Get the queues
799 mlt_deque queue = mlt_properties_get_data( properties, "frame_queue", NULL );
800 sample_fifo fifo = mlt_properties_get_data( properties, "sample_fifo", NULL );
801
802 // Need two av pictures for converting
803 AVFrame *output = NULL;
804 AVFrame *input = alloc_picture( PIX_FMT_YUV422, width, height );
805
806 // For receiving images from an mlt_frame
807 uint8_t *image;
808 mlt_image_format img_fmt = mlt_image_yuv422;
809
810 // For receiving audio samples back from the fifo
811 int16_t *buffer = av_malloc( 48000 * 2 );
812 int count = 0;
813
814 // Allocate the context
815 AVFormatContext *oc = av_alloc_format_context( );
816
817 // Streams
818 AVStream *audio_st = NULL;
819 AVStream *video_st = NULL;
820
821 // Time stamps
822 double audio_pts = 0;
823 double video_pts = 0;
824
825 // Loop variable
826 int i;
827
828 // Frames despatched
829 long int frames = 0;
830 long int total_time = 0;
831
832 // Determine the format
833 AVOutputFormat *fmt = NULL;
834 char *filename = mlt_properties_get( properties, "target" );
835 char *format = mlt_properties_get( properties, "f" );
836 char *vcodec = mlt_properties_get( properties, "vcodec" );
837 char *acodec = mlt_properties_get( properties, "acodec" );
838
839 // Used to store and override codec ids
840 int audio_codec_id;
841 int video_codec_id;
842
843 // Check for user selected format first
844 if ( format != NULL )
845 fmt = guess_format( format, NULL, NULL );
846
847 // Otherwise check on the filename
848 if ( fmt == NULL && filename != NULL )
849 fmt = guess_format( NULL, filename, NULL );
850
851 // Otherwise default to mpeg
852 if ( fmt == NULL )
853 fmt = guess_format( "mpeg", NULL, NULL );
854
855 // We need a filename - default to stdout?
856 if ( filename == NULL || !strcmp( filename, "" ) )
857 filename = "pipe:";
858
859 // Get the codec ids selected
860 audio_codec_id = fmt->audio_codec;
861 video_codec_id = fmt->video_codec;
862
863 // Check for audio codec overides
864 if ( acodec != NULL )
865 {
866 AVCodec *p = avcodec_find_encoder_by_name( acodec );
867 if ( p != NULL )
868 audio_codec_id = p->id;
869 else
870 fprintf( stderr, "%s: audio codec %s unrecognised - ignoring\n", __FILE__, acodec );
871 }
872
873 // Check for video codec overides
874 if ( vcodec != NULL )
875 {
876 AVCodec *p = avcodec_find_encoder_by_name( vcodec );
877 if ( p != NULL )
878 video_codec_id = p->id;
879 else
880 fprintf( stderr, "%s: video codec %s unrecognised - ignoring\n", __FILE__, vcodec );
881 }
882
883 // Write metadata
884 char *tmp = NULL;
885 int metavalue;
886
887 tmp = mlt_properties_get( properties, "meta.attr.title.markup");
888 if (tmp != NULL) snprintf( oc->title, sizeof(oc->title), "%s", tmp );
889
890 tmp = mlt_properties_get( properties, "meta.attr.comment.markup");
891 if (tmp != NULL) snprintf( oc->comment, sizeof(oc->comment), "%s", tmp );
892
893 tmp = mlt_properties_get( properties, "meta.attr.author.markup");
894 if (tmp != NULL) snprintf( oc->author, sizeof(oc->author), "%s", tmp );
895
896 tmp = mlt_properties_get( properties, "meta.attr.copyright.markup");
897 if (tmp != NULL) snprintf( oc->copyright, sizeof(oc->copyright), "%s", tmp );
898
899 tmp = mlt_properties_get( properties, "meta.attr.album.markup");
900 if (tmp != NULL) snprintf( oc->album, sizeof(oc->album), "%s", tmp );
901
902 metavalue = mlt_properties_get_int( properties, "meta.attr.year.markup");
903 if (metavalue != 0) oc->year = metavalue;
904
905 metavalue = mlt_properties_get_int( properties, "meta.attr.track.markup");
906 if (metavalue != 0) oc->track = metavalue;
907
908 oc->oformat = fmt;
909 snprintf( oc->filename, sizeof(oc->filename), "%s", filename );
910
911 // Add audio and video streams
912 if ( fmt->video_codec != CODEC_ID_NONE )
913 video_st = add_video_stream( this, oc, video_codec_id );
914 if ( fmt->audio_codec != CODEC_ID_NONE )
915 audio_st = add_audio_stream( this, oc, audio_codec_id );
916
917 // Set the parameters (even though we have none...)
918 if ( av_set_parameters(oc, NULL) >= 0 )
919 {
920 oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
921 oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
922
923 // Process properties as AVOptions
924 apply_properties( oc, properties, AV_OPT_FLAG_ENCODING_PARAM );
925
926 if ( video_st && !open_video( oc, video_st ) )
927 video_st = NULL;
928 if ( audio_st )
929 audio_input_frame_size = open_audio( oc, audio_st, audio_outbuf_size );
930
931 // Open the output file, if needed
932 if ( !( fmt->flags & AVFMT_NOFILE ) )
933 {
934 if ( url_fopen( &oc->pb, filename, URL_WRONLY ) < 0 )
935 {
936 fprintf( stderr, "%s: Could not open '%s'\n", __FILE__, filename );
937 mlt_properties_set_int( properties, "running", 0 );
938 }
939 }
940
941 // Write the stream header, if any
942 if ( mlt_properties_get_int( properties, "running" ) )
943 av_write_header( oc );
944 }
945 else
946 {
947 fprintf( stderr, "%s: Invalid output format parameters\n", __FILE__ );
948 mlt_properties_set_int( properties, "running", 0 );
949 }
950
951 // Allocate picture
952 if ( video_st )
953 output = alloc_picture( video_st->codec->pix_fmt, width, height );
954
955 // Last check - need at least one stream
956 if ( audio_st == NULL && video_st == NULL )
957 mlt_properties_set_int( properties, "running", 0 );
958
959 // Get the starting time (can ignore the times above)
960 gettimeofday( &ante, NULL );
961
962 // Loop while running
963 while( mlt_properties_get_int( properties, "running" ) && !terminated )
964 {
965 // Get the frame
966 frame = mlt_consumer_rt_frame( this );
967
968 // Check that we have a frame to work with
969 if ( frame != NULL )
970 {
971 // Increment frames despatched
972 frames ++;
973
974 // Default audio args
975 frame_properties = MLT_FRAME_PROPERTIES( frame );
976
977 // Check for the terminated condition
978 terminated = terminate_on_pause && mlt_properties_get_double( frame_properties, "_speed" ) == 0.0;
979
980 // Get audio and append to the fifo
981 if ( !terminated && audio_st )
982 {
983 samples = mlt_sample_calculator( fps, frequency, count ++ );
984 mlt_frame_get_audio( frame, &pcm, &aud_fmt, &frequency, &channels, &samples );
985
986 // Create the fifo if we don't have one
987 if ( fifo == NULL )
988 {
989 fifo = sample_fifo_init( frequency, channels );
990 mlt_properties_set_data( properties, "sample_fifo", fifo, 0, ( mlt_destructor )sample_fifo_close, NULL );
991 }
992
993 if ( mlt_properties_get_double( frame_properties, "_speed" ) != 1.0 )
994 memset( pcm, 0, samples * channels * 2 );
995
996 // Append the samples
997 sample_fifo_append( fifo, pcm, samples * channels );
998 total_time += ( samples * 1000000 ) / frequency;
999 }
1000
1001 // Encode the image
1002 if ( !terminated && video_st )
1003 mlt_deque_push_back( queue, frame );
1004 else
1005 mlt_frame_close( frame );
1006 }
1007
1008 // While we have stuff to process, process...
1009 while ( 1 )
1010 {
1011 if (audio_st)
1012 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
1013 else
1014 audio_pts = 0.0;
1015
1016 if (video_st)
1017 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
1018 else
1019 video_pts = 0.0;
1020
1021 // Write interleaved audio and video frames
1022 if ( !video_st || ( video_st && audio_st && audio_pts < video_pts ) )
1023 {
1024 if ( channels * audio_input_frame_size < sample_fifo_used( fifo ) )
1025 {
1026 AVCodecContext *c;
1027 AVPacket pkt;
1028 av_init_packet( &pkt );
1029
1030 c = audio_st->codec;
1031
1032 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1033
1034 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1035 // Write the compressed frame in the media file
1036 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1037 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1038 pkt.flags |= PKT_FLAG_KEY;
1039 pkt.stream_index= audio_st->index;
1040 pkt.data= audio_outbuf;
1041
1042 if ( pkt.size )
1043 if ( av_interleaved_write_frame( oc, &pkt ) != 0)
1044 fprintf( stderr, "%s: Error while writing audio frame\n", __FILE__ );
1045
1046 audio_pts += c->frame_size;
1047 }
1048 else
1049 {
1050 break;
1051 }
1052 }
1053 else if ( video_st )
1054 {
1055 if ( mlt_deque_count( queue ) )
1056 {
1057 int out_size, ret;
1058 AVCodecContext *c;
1059
1060 frame = mlt_deque_pop_front( queue );
1061 frame_properties = MLT_FRAME_PROPERTIES( frame );
1062
1063 c = video_st->codec;
1064
1065 if ( mlt_properties_get_int( frame_properties, "rendered" ) )
1066 {
1067 int i = 0;
1068 int j = 0;
1069 uint8_t *p;
1070 uint8_t *q;
1071
1072 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
1073
1074 mlt_frame_get_image( frame, &image, &img_fmt, &img_width, &img_height, 0 );
1075
1076 q = image;
1077
1078 // Convert the mlt frame to an AVPicture
1079 for ( i = 0; i < height; i ++ )
1080 {
1081 p = input->data[ 0 ] + i * input->linesize[ 0 ];
1082 j = width;
1083 while( j -- )
1084 {
1085 *p ++ = *q ++;
1086 *p ++ = *q ++;
1087 }
1088 }
1089
1090 // Do the colour space conversion
1091 #ifdef SWSCALE
1092 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV422,
1093 width, height, video_st->codec->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
1094 sws_scale( context, input->data, input->linesize, 0, height,
1095 output->data, output->linesize);
1096 sws_freeContext( context );
1097 #else
1098 img_convert( ( AVPicture * )output, video_st->codec->pix_fmt, ( AVPicture * )input, PIX_FMT_YUV422, width, height );
1099 #endif
1100
1101 // Apply the alpha if applicable
1102 if ( video_st->codec->pix_fmt == PIX_FMT_RGBA32 )
1103 {
1104 uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
1105 register int n;
1106
1107 for ( i = 0; i < height; i ++ )
1108 {
1109 n = ( width + 7 ) / 8;
1110 p = output->data[ 0 ] + i * output->linesize[ 0 ];
1111
1112 #ifndef __DARWIN__
1113 p += 3;
1114 #endif
1115
1116 switch( width % 8 )
1117 {
1118 case 0: do { *p = *alpha++; p += 4;
1119 case 7: *p = *alpha++; p += 4;
1120 case 6: *p = *alpha++; p += 4;
1121 case 5: *p = *alpha++; p += 4;
1122 case 4: *p = *alpha++; p += 4;
1123 case 3: *p = *alpha++; p += 4;
1124 case 2: *p = *alpha++; p += 4;
1125 case 1: *p = *alpha++; p += 4;
1126 }
1127 while( --n );
1128 }
1129 }
1130 }
1131 }
1132
1133 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1134 {
1135 // raw video case. The API will change slightly in the near future for that
1136 AVPacket pkt;
1137 av_init_packet(&pkt);
1138
1139 pkt.flags |= PKT_FLAG_KEY;
1140 pkt.stream_index= video_st->index;
1141 pkt.data= (uint8_t *)output;
1142 pkt.size= sizeof(AVPicture);
1143
1144 ret = av_write_frame(oc, &pkt);
1145 video_pts += c->frame_size;
1146 }
1147 else
1148 {
1149 // Set the quality
1150 output->quality = video_st->quality;
1151
1152 // Set frame interlace hints
1153 output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
1154 output->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1155
1156 // Encode the image
1157 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, output );
1158
1159 // If zero size, it means the image was buffered
1160 if (out_size > 0)
1161 {
1162 AVPacket pkt;
1163 av_init_packet( &pkt );
1164
1165 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1166 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1167 if( c->coded_frame && c->coded_frame->key_frame )
1168 pkt.flags |= PKT_FLAG_KEY;
1169 pkt.stream_index= video_st->index;
1170 pkt.data= video_outbuf;
1171 pkt.size= out_size;
1172
1173 // write the compressed frame in the media file
1174 ret = av_interleaved_write_frame(oc, &pkt);
1175 video_pts += c->frame_size;
1176
1177 // Dual pass logging
1178 if ( mlt_properties_get_data( properties, "_logfile", NULL ) && c->stats_out)
1179 fprintf( mlt_properties_get_data( properties, "_logfile", NULL ), "%s", c->stats_out );
1180 }
1181 else
1182 {
1183 fprintf( stderr, "%s: error with video encode\n", __FILE__ );
1184 }
1185 }
1186 frame_count++;
1187 mlt_frame_close( frame );
1188 }
1189 else
1190 {
1191 break;
1192 }
1193 }
1194 }
1195
1196 if ( real_time_output == 1 && frames % 12 == 0 )
1197 {
1198 long passed = time_difference( &ante );
1199 if ( fifo != NULL )
1200 {
1201 long pending = ( ( ( long )sample_fifo_used( fifo ) * 1000 ) / frequency ) * 1000;
1202 passed -= pending;
1203 }
1204 if ( passed < total_time )
1205 {
1206 long total = ( total_time - passed );
1207 struct timespec t = { total / 1000000, ( total % 1000000 ) * 1000 };
1208 nanosleep( &t, NULL );
1209 }
1210 }
1211 }
1212
1213 #ifdef FLUSH
1214 if ( ! real_time_output )
1215 {
1216 // Flush audio fifo
1217 if ( audio_st && audio_st->codec->frame_size > 1 ) for (;;)
1218 {
1219 AVCodecContext *c = audio_st->codec;
1220 AVPacket pkt;
1221 av_init_packet( &pkt );
1222 pkt.size = 0;
1223
1224 if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
1225 ( channels * audio_input_frame_size < sample_fifo_used( fifo ) ) )
1226 {
1227 sample_fifo_fetch( fifo, buffer, channels * audio_input_frame_size );
1228 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, buffer );
1229 }
1230 if ( pkt.size <= 0 )
1231 pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, NULL );
1232 if ( pkt.size <= 0 )
1233 break;
1234
1235 // Write the compressed frame in the media file
1236 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1237 pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, audio_st->time_base );
1238 pkt.flags |= PKT_FLAG_KEY;
1239 pkt.stream_index = audio_st->index;
1240 pkt.data = audio_outbuf;
1241 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1242 {
1243 fprintf( stderr, "%s: Error while writing flushed audio frame\n", __FILE__ );
1244 break;
1245 }
1246 }
1247
1248 // Flush video
1249 if ( video_st && !( oc->oformat->flags & AVFMT_RAWPICTURE ) ) for (;;)
1250 {
1251 AVCodecContext *c = video_st->codec;
1252 AVPacket pkt;
1253 av_init_packet( &pkt );
1254
1255 // Encode the image
1256 pkt.size = avcodec_encode_video( c, video_outbuf, video_outbuf_size, NULL );
1257 if ( pkt.size <= 0 )
1258 break;
1259
1260 if ( c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE )
1261 pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, video_st->time_base );
1262 if( c->coded_frame && c->coded_frame->key_frame )
1263 pkt.flags |= PKT_FLAG_KEY;
1264 pkt.stream_index = video_st->index;
1265 pkt.data = video_outbuf;
1266
1267 // write the compressed frame in the media file
1268 if ( av_interleaved_write_frame( oc, &pkt ) != 0 )
1269 {
1270 fprintf( stderr, "%s: Error while writing flushed video frame\n". __FILE__ );
1271 break;
1272 }
1273 }
1274 }
1275 #endif
1276
1277 // close each codec
1278 if (video_st)
1279 close_video(oc, video_st);
1280 if (audio_st)
1281 close_audio(oc, audio_st);
1282
1283 // Write the trailer, if any
1284 av_write_trailer(oc);
1285
1286 // Free the streams
1287 for(i = 0; i < oc->nb_streams; i++)
1288 av_freep(&oc->streams[i]);
1289
1290 // Close the output file
1291 if (!(fmt->flags & AVFMT_NOFILE))
1292 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
1293 url_fclose(oc->pb);
1294 #else
1295 url_fclose(&oc->pb);
1296 #endif
1297
1298 // Clean up input and output frames
1299 if ( output )
1300 av_free( output->data[0] );
1301 av_free( output );
1302 av_free( input->data[0] );
1303 av_free( input );
1304 av_free( video_outbuf );
1305 av_free( buffer );
1306
1307 // Free the stream
1308 av_free(oc);
1309
1310 // Just in case we terminated on pause
1311 mlt_properties_set_int( properties, "running", 0 );
1312
1313 mlt_consumer_stopped( this );
1314
1315 if ( mlt_properties_get_int( properties, "pass" ) == 2 )
1316 {
1317 // Remove the dual pass log file
1318 if ( mlt_properties_get( properties, "_logfilename" ) )
1319 remove( mlt_properties_get( properties, "_logfilename" ) );
1320
1321 // Remove the x264 dual pass logs
1322 char *cwd = getcwd( NULL, 0 );
1323 char *file = "x264_2pass.log";
1324 char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1325 sprintf( full, "%s/%s", cwd, file );
1326 remove( full );
1327 free( full );
1328 file = "x264_2pass.log.temp";
1329 full = malloc( strlen( cwd ) + strlen( file ) + 2 );
1330 sprintf( full, "%s/%s", cwd, file );
1331 remove( full );
1332 free( full );
1333 free( cwd );
1334 remove( "x264_2pass.log.temp" );
1335 }
1336
1337 return NULL;
1338 }
1339
1340 /** Close the consumer.
1341 */
1342
1343 static void consumer_close( mlt_consumer this )
1344 {
1345 // Stop the consumer
1346 mlt_consumer_stop( this );
1347
1348 // Close the parent
1349 mlt_consumer_close( this );
1350
1351 // Free the memory
1352 free( this );
1353 }