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