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