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
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.
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.
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
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
26 // ffmpeg Header files
31 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
32 # include "audioconvert.h"
35 // System header files
41 void avformat_lock( );
42 void avformat_unlock( );
44 // Forward references.
45 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
);
46 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
48 /** Constructor for libavformat.
51 mlt_producer
producer_avformat_init( mlt_profile profile
, char *file
)
53 mlt_producer
this = NULL
;
55 // Check that we have a non-NULL argument
58 // Construct the producer
59 this = calloc( 1, sizeof( struct mlt_producer_s
) );
62 if ( mlt_producer_init( this, NULL
) == 0 )
65 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
67 // Set the resource property (required for all producers)
68 mlt_properties_set( properties
, "resource", file
);
70 // Register our get_frame implementation
71 this->get_frame
= producer_get_frame
;
74 if ( producer_open( this, profile
, file
) != 0 )
77 mlt_producer_close( this );
82 // Close the file to release resources for large playlists - reopen later as needed
83 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
84 mlt_properties_set_data( properties
, "audio_context", NULL
, 0, NULL
, NULL
);
85 mlt_properties_set_data( properties
, "video_context", NULL
, 0, NULL
, NULL
);
87 // Default the user-selectable indices from the auto-detected indices
88 mlt_properties_set_int( properties
, "audio_index", mlt_properties_get_int( properties
, "_audio_index" ) );
89 mlt_properties_set_int( properties
, "video_index", mlt_properties_get_int( properties
, "_video_index" ) );
97 /** Find the default streams.
100 static void find_default_streams( AVFormatContext
*context
, int *audio_index
, int *video_index
)
104 // Allow for multiple audio and video streams in the file and select first of each (if available)
105 for( i
= 0; i
< context
->nb_streams
; i
++ )
107 // Get the codec context
108 AVCodecContext
*codec_context
= context
->streams
[ i
]->codec
;
110 if ( avcodec_find_decoder( codec_context
->codec_id
) == NULL
)
113 // Determine the type and obtain the first index of each type
114 switch( codec_context
->codec_type
)
116 case CODEC_TYPE_VIDEO
:
117 if ( *video_index
< 0 )
120 case CODEC_TYPE_AUDIO
:
121 if ( *audio_index
< 0 )
130 /** Producer file destructor.
133 static void producer_file_close( void *context
)
135 if ( context
!= NULL
)
137 // Lock the mutex now
141 av_close_input_file( context
);
143 // Unlock the mutex now
148 /** Producer file destructor.
151 static void producer_codec_close( void *codec
)
155 // Lock the mutex now
159 avcodec_close( codec
);
161 // Unlock the mutex now
166 static inline int dv_is_pal( AVPacket
*pkt
)
168 return pkt
->data
[3] & 0x80;
171 static int dv_is_wide( AVPacket
*pkt
)
173 int i
= 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
175 for ( ; i
< pkt
->size
; i
+= 5 /* packet size */ )
177 if ( pkt
->data
[ i
] == 0x61 )
179 uint8_t x
= pkt
->data
[ i
+ 2 ] & 0x7;
180 return ( x
== 2 ) || ( x
== 7 );
186 static double get_aspect_ratio( AVStream
*stream
, AVCodecContext
*codec_context
, AVPacket
*pkt
)
188 double aspect_ratio
= 1.0;
190 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
194 if ( dv_is_pal( pkt
) )
196 aspect_ratio
= dv_is_wide( pkt
)
197 ?
64.0/45.0 // 16:9 PAL
198 : 16.0/15.0; // 4:3 PAL
202 aspect_ratio
= dv_is_wide( pkt
)
203 ?
32.0/27.0 // 16:9 NTSC
204 : 8.0/9.0; // 4:3 NTSC
210 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
211 stream
->sample_aspect_ratio
;
213 codec_context
->sample_aspect_ratio
;
215 // Override FFmpeg's notion of DV aspect ratios, which are
216 // based upon a width of 704. Since we do not have a normaliser
217 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
218 // we just coerce the values to facilitate a passive behaviour through
219 // the rescale normaliser when using equivalent producers and consumers.
220 // = display_aspect / (width * height)
221 if ( ar
.num
== 10 && ar
.den
== 11 )
222 aspect_ratio
= 8.0/9.0; // 4:3 NTSC
223 else if ( ar
.num
== 59 && ar
.den
== 54 )
224 aspect_ratio
= 16.0/15.0; // 4:3 PAL
225 else if ( ar
.num
== 40 && ar
.den
== 33 )
226 aspect_ratio
= 32.0/27.0; // 16:9 NTSC
227 else if ( ar
.num
== 118 && ar
.den
== 81 )
228 aspect_ratio
= 64.0/45.0; // 16:9 PAL
233 AVRational codec_sar
= codec_context
->sample_aspect_ratio
;
234 AVRational stream_sar
=
235 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
236 stream
->sample_aspect_ratio
;
240 if ( codec_sar
.num
> 0 )
241 aspect_ratio
= av_q2d( codec_sar
);
242 else if ( stream_sar
.num
> 0 )
243 aspect_ratio
= av_q2d( stream_sar
);
251 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
253 // Return an error code (0 == no error)
256 // Context for avformat
257 AVFormatContext
*context
= NULL
;
259 // Get the properties
260 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
262 // We will treat everything with the producer fps
263 double fps
= mlt_profile_fps( profile
);
265 // Lock the mutex now
268 // If "MRL", then create AVInputFormat
269 AVInputFormat
*format
= NULL
;
270 AVFormatParameters
*params
= NULL
;
271 char *standard
= NULL
;
272 char *mrl
= strchr( file
, ':' );
274 // AV option (0 = both, 1 = video, 2 = audio)
277 // Setting lowest log level
278 av_log_set_level( -1 );
280 // Only if there is not a protocol specification that avformat can handle
281 if ( mrl
&& !url_exist( file
) )
283 // 'file' becomes format abbreviation
287 format
= av_find_input_format( file
);
289 // Eat the format designator
295 params
= calloc( sizeof( AVFormatParameters
), 1 );
297 // These are required by video4linux (defaults)
299 params
->height
= 480;
300 params
->time_base
= (AVRational
){1,25};
301 // params->device = file;
302 params
->channels
= 2;
303 params
->sample_rate
= 48000;
306 // XXX: this does not work anymore since avdevice
307 // TODO: make producer_avddevice?
309 mrl
= strchr( file
, '?' );
313 char *name
= strdup( ++mrl
);
314 char *value
= strchr( name
, ':' );
319 char *t
= strchr( value
, '&' );
322 if ( !strcmp( name
, "frame_rate" ) )
323 params
->time_base
.den
= atoi( value
);
324 else if ( !strcmp( name
, "frame_rate_base" ) )
325 params
->time_base
.num
= atoi( value
);
326 else if ( !strcmp( name
, "sample_rate" ) )
327 params
->sample_rate
= atoi( value
);
328 else if ( !strcmp( name
, "channels" ) )
329 params
->channels
= atoi( value
);
330 else if ( !strcmp( name
, "width" ) )
331 params
->width
= atoi( value
);
332 else if ( !strcmp( name
, "height" ) )
333 params
->height
= atoi( value
);
334 else if ( !strcmp( name
, "standard" ) )
336 standard
= strdup( value
);
337 params
->standard
= standard
;
339 else if ( !strcmp( name
, "av" ) )
343 mrl
= strchr( mrl
, '&' );
347 // Now attempt to open the file
348 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
350 // Cleanup AVFormatParameters
354 // If successful, then try to get additional info
357 // Get the stream info
358 error
= av_find_stream_info( context
) < 0;
360 // Continue if no error
363 // We will default to the first audio and video streams found
364 int audio_index
= -1;
365 int video_index
= -1;
368 // Now set properties where we can (use default unknowns if required)
369 if ( context
->duration
!= AV_NOPTS_VALUE
)
371 // This isn't going to be accurate for all formats
372 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
+ 0.5 );
373 mlt_properties_set_position( properties
, "out", frames
- 1 );
374 mlt_properties_set_position( properties
, "length", frames
);
377 // Find default audio and video streams
378 find_default_streams( context
, &audio_index
, &video_index
);
380 if ( context
->start_time
!= AV_NOPTS_VALUE
)
381 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
383 // Check if we're seekable (something funny about mpeg here :-/)
384 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) && strncmp( file
, "udp:", 4 ) && strncmp( file
, "tcp:", 4 ) && strncmp( file
, "rtsp:", 5 ) && strncmp( file
, "rtp:", 4 ) )
386 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
387 mlt_properties_set_data( properties
, "dummy_context", context
, 0, producer_file_close
, NULL
);
388 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
389 av_find_stream_info( context
);
394 // Store selected audio and video indexes on properties
395 mlt_properties_set_int( properties
, "_audio_index", audio_index
);
396 mlt_properties_set_int( properties
, "_video_index", video_index
);
397 mlt_properties_set_int( properties
, "_last_position", -1 );
399 // Fetch the width, height and aspect ratio
400 if ( video_index
!= -1 )
402 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
403 mlt_properties_set_int( properties
, "width", codec_context
->width
);
404 mlt_properties_set_int( properties
, "height", codec_context
->height
);
406 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
408 // Fetch the first frame of DV so we can read it directly
413 ret
= av_read_frame( context
, &pkt
);
414 if ( ret
>= 0 && pkt
.stream_index
== video_index
&& pkt
.size
> 0 )
416 mlt_properties_set_double( properties
, "aspect_ratio",
417 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, &pkt
) );
424 mlt_properties_set_double( properties
, "aspect_ratio",
425 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, NULL
) );
430 if (context
->title
!= NULL
)
431 mlt_properties_set(properties
, "meta.attr.title.markup", context
->title
);
432 if (context
->author
!= NULL
)
433 mlt_properties_set(properties
, "meta.attr.author.markup", context
->author
);
434 if (context
->copyright
!= NULL
)
435 mlt_properties_set(properties
, "meta.attr.copyright.markup", context
->copyright
);
436 if (context
->comment
!= NULL
)
437 mlt_properties_set(properties
, "meta.attr.comment.markup", context
->comment
);
438 if (context
->album
!= NULL
)
439 mlt_properties_set(properties
, "meta.attr.album.markup", context
->album
);
440 if (context
->year
!= 0)
441 mlt_properties_set_int(properties
, "meta.attr.year.markup", context
->year
);
442 if (context
->track
!= 0)
443 mlt_properties_set_int(properties
, "meta.attr.track.markup", context
->track
);
445 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
446 if ( av
== 0 && audio_index
!= -1 && video_index
!= -1 )
448 // We'll use the open one as our video_context
449 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
451 // And open again for our audio context
452 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
453 av_find_stream_info( context
);
456 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
458 else if ( av
!= 2 && video_index
!= -1 )
460 // We only have a video context
461 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
463 else if ( audio_index
!= -1 )
465 // We only have an audio context
466 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
470 // Something has gone wrong
474 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
478 // Unlock the mutex now
484 /** Convert a frame position to a time code.
487 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
489 return ( double )position
/ mlt_producer_get_fps( this );
492 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
495 if ( format
== mlt_image_yuv420p
)
497 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
498 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
500 output
.data
[0] = buffer
;
501 output
.data
[1] = buffer
+ width
* height
;
502 output
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
503 output
.linesize
[0] = width
;
504 output
.linesize
[1] = width
>> 1;
505 output
.linesize
[2] = width
>> 1;
506 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
507 output
.data
, output
.linesize
);
508 sws_freeContext( context
);
510 else if ( format
== mlt_image_rgb24
)
512 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
513 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
515 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
516 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
517 output
.data
, output
.linesize
);
518 sws_freeContext( context
);
522 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
523 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
525 avpicture_fill( &output
, buffer
, PIX_FMT_YUYV422
, width
, height
);
526 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
527 output
.data
, output
.linesize
);
528 sws_freeContext( context
);
531 if ( format
== mlt_image_yuv420p
)
534 pict
.data
[0] = buffer
;
535 pict
.data
[1] = buffer
+ width
* height
;
536 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
537 pict
.linesize
[0] = width
;
538 pict
.linesize
[1] = width
>> 1;
539 pict
.linesize
[2] = width
>> 1;
540 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
542 else if ( format
== mlt_image_rgb24
)
545 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
546 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
551 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
552 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
557 /** Allocate the image buffer and set it on the frame.
560 static int allocate_buffer( mlt_properties frame_properties
, AVCodecContext
*codec_context
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
)
564 if ( codec_context
->width
== 0 || codec_context
->height
== 0 )
567 *width
= codec_context
->width
;
568 *height
= codec_context
->height
;
569 mlt_properties_set_int( frame_properties
, "width", *width
);
570 mlt_properties_set_int( frame_properties
, "height", *height
);
574 case mlt_image_yuv420p
:
575 size
= *width
* 3 * ( *height
+ 1 ) / 2;
577 case mlt_image_rgb24
:
578 size
= *width
* ( *height
+ 1 ) * 3;
581 *format
= mlt_image_yuv422
;
582 size
= *width
* ( *height
+ 1 ) * 2;
586 // Construct the output image
587 *buffer
= mlt_pool_alloc( size
);
589 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
596 /** Get an image from a frame.
599 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
601 // Get the properties from the frame
602 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
604 // Obtain the frame number of this frame
605 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
608 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
610 // Get the producer properties
611 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
613 // Fetch the video_context
614 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
616 // Get the video_index
617 int index
= mlt_properties_get_int( properties
, "video_index" );
619 // Obtain the expected frame numer
620 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
622 // Get the video stream
623 AVStream
*stream
= context
->streams
[ index
];
626 AVCodecContext
*codec_context
= stream
->codec
;
631 // Get the conversion frame
632 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
634 // Special case pause handling flag
637 // Special case ffwd handling
640 // We may want to use the source fps if available
641 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
642 double fps
= mlt_producer_get_fps( this );
644 // This is the physical frame position in the source
645 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
647 // Get the seekable status
648 int seekable
= mlt_properties_get_int( properties
, "seekable" );
650 // Hopefully provide better support for streams...
651 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
653 // Determines if we have to decode all frames in a sequence
656 // Temporary hack to improve intra frame only
657 must_decode
= strcmp( codec_context
->codec
->name
, "mjpeg" ) &&
658 strcmp( codec_context
->codec
->name
, "rawvideo" ) &&
659 strcmp( codec_context
->codec
->name
, "dvvideo" );
662 if ( position
!= expected
)
664 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
666 // We're paused - use last image
669 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
671 // Fast forward - seeking is inefficient for small distances - just ignore following frames
672 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
674 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
676 // Calculate the timestamp for the requested frame
677 int64_t timestamp
= ( int64_t )( ( double )req_position
/ source_fps
* AV_TIME_BASE
+ 0.5 );
678 if ( ( uint64_t )context
->start_time
!= AV_NOPTS_VALUE
)
679 timestamp
+= context
->start_time
;
681 timestamp
-= AV_TIME_BASE
;
685 // Set to the timestamp
686 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
688 // Remove the cached info relating to the previous position
689 mlt_properties_set_int( properties
, "_current_position", -1 );
690 mlt_properties_set_int( properties
, "_last_position", -1 );
691 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
696 // Duplicate the last image if necessary (see comment on rawvideo below)
697 int current_position
= mlt_properties_get_int( properties
, "_current_position" );
698 int got_picture
= mlt_properties_get_int( properties
, "_got_picture" );
699 if ( av_frame
!= NULL
&& got_picture
&& ( paused
|| current_position
>= req_position
) && av_bypass
== 0 )
702 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
703 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
705 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
710 int int_position
= 0;
713 av_init_packet( &pkt
);
715 // Construct an AVFrame for YUV422 conversion
716 if ( av_frame
== NULL
)
717 av_frame
= avcodec_alloc_frame( );
719 while( ret
>= 0 && !got_picture
)
722 ret
= av_read_frame( context
, &pkt
);
724 // We only deal with video from the selected video_index
725 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
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
);
737 if ( must_decode
|| int_position
>= req_position
)
738 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
743 if ( int_position
< req_position
)
748 else if ( int_position
>= req_position
)
752 else if ( ignore
-- )
757 av_free_packet( &pkt
);
761 av_free_packet( &pkt
);
764 // Now handle the picture if we have one
767 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
769 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
770 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
771 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
772 mlt_properties_set_int( properties
, "_current_position", int_position
);
773 mlt_properties_set_int( properties
, "_got_picture", 1 );
774 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
783 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
786 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
787 // above will break the pause behaviour - so we wipe the frame now
788 if ( !strcmp( codec_context
->codec
->name
, "rawvideo" ) )
789 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
791 // Set the field order property for this frame
792 mlt_properties_set_int( frame_properties
, "top_field_first", mlt_properties_get_int( properties
, "top_field_first" ) );
794 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
795 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
800 /** Set up video handling.
803 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
805 // Get the properties
806 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
808 // Fetch the video_context
809 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
811 // Get the video_index
812 int index
= mlt_properties_get_int( properties
, "video_index" );
814 // Reopen the file if necessary
815 if ( !context
&& index
> -1 )
817 mlt_events_block( properties
, this );
818 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
819 mlt_properties_get( properties
, "resource" ) );
820 context
= mlt_properties_get_data( properties
, "video_context", NULL
);
821 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
822 mlt_events_unblock( properties
, this );
825 // Exception handling for video_index
826 if ( context
&& index
>= (int) context
->nb_streams
)
828 // Get the last video stream
829 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
; --index
);
830 mlt_properties_set_int( properties
, "video_index", index
);
832 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
)
834 // Invalidate the video stream
836 mlt_properties_set_int( properties
, "video_index", index
);
839 // Get the frame properties
840 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
842 if ( context
&& index
> -1 )
844 // Get the video stream
845 AVStream
*stream
= context
->streams
[ index
];
848 AVCodecContext
*codec_context
= stream
->codec
;
851 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
853 // Update the video properties if the index changed
854 if ( index
!= mlt_properties_get_int( properties
, "_video_index" ) )
856 // Reset the video properties if the index changed
857 mlt_properties_set_int( properties
, "_video_index", index
);
858 mlt_properties_set_data( properties
, "video_codec", NULL
, 0, NULL
, NULL
);
859 mlt_properties_set_int( properties
, "width", codec_context
->width
);
860 mlt_properties_set_int( properties
, "height", codec_context
->height
);
861 // TODO: get the first usable AVPacket and reset the stream position
862 mlt_properties_set_double( properties
, "aspect_ratio",
863 get_aspect_ratio( context
->streams
[ index
], codec_context
, NULL
) );
867 // Initialise the codec if necessary
870 // Initialise multi-threading
871 int thread_count
= mlt_properties_get_int( properties
, "threads" );
872 if ( thread_count
== 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
873 thread_count
= atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
874 if ( thread_count
> 1 )
876 avcodec_thread_init( codec_context
, thread_count
);
877 codec_context
->thread_count
= thread_count
;
881 codec
= avcodec_find_decoder( codec_context
->codec_id
);
883 // If we don't have a codec and we can't initialise it, we can't do much more...
885 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
887 // Now store the codec with its destructor
888 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
892 // Remember that we can't use this later
893 mlt_properties_set_int( properties
, "video_index", -1 );
899 // No codec, no show...
900 if ( codec
&& index
> -1 )
902 double source_fps
= 0;
903 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
904 double aspect_ratio
= ( force_aspect_ratio
> 0.0 ) ?
905 force_aspect_ratio
: mlt_properties_get_double( properties
, "aspect_ratio" );
908 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
910 // We'll use fps if it's available
911 if ( source_fps
> 0 )
912 mlt_properties_set_double( properties
, "source_fps", source_fps
);
914 mlt_properties_set_double( properties
, "source_fps", mlt_producer_get_fps( this ) );
915 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
917 // Set the width and height
918 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
919 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
920 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
922 mlt_frame_push_get_image( frame
, producer_get_image
);
923 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
927 mlt_properties_set_int( frame_properties
, "test_image", 1 );
932 mlt_properties_set_int( frame_properties
, "test_image", 1 );
936 /** Get the audio from a frame.
939 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
941 // Get the properties from the frame
942 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
944 // Obtain the frame number of this frame
945 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
948 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
950 // Get the producer properties
951 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
953 // Fetch the audio_context
954 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
956 // Get the audio_index
957 int index
= mlt_properties_get_int( properties
, "audio_index" );
959 // Get the seekable status
960 int seekable
= mlt_properties_get_int( properties
, "seekable" );
962 // Obtain the expected frame numer
963 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
965 // Obtain the resample context if it exists (not always needed)
966 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
968 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
969 // Get the format converter context if it exists
970 AVAudioConvert
*convert
= mlt_properties_get_data( properties
, "audio_convert", NULL
);
973 // Obtain the audio buffers
974 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
975 int16_t *decode_buffer
= mlt_properties_get_data( properties
, "decode_buffer", NULL
);
976 int16_t *convert_buffer
= mlt_properties_get_data( properties
, "convert_buffer", NULL
);
978 // Get amount of audio used
979 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
981 // Calculate the real time code
982 double real_timecode
= producer_time_of_frame( this, position
);
984 // Get the audio stream
985 AVStream
*stream
= context
->streams
[ index
];
988 AVCodecContext
*codec_context
= stream
->codec
;
993 // Number of frames to ignore (for ffwd)
996 // Flag for paused (silence)
999 // Check for resample and create if necessary
1000 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
1002 // Create the resampler
1003 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
1005 // And store it on properties
1006 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
1008 else if ( resample
== NULL
)
1010 *channels
= codec_context
->channels
;
1011 *frequency
= codec_context
->sample_rate
;
1014 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1015 // Check for audio format converter and create if necessary
1016 // TODO: support higher resolutions than 16-bit.
1017 if ( convert
== NULL
&& codec_context
->sample_fmt
!= SAMPLE_FMT_S16
)
1019 // Create single channel converter for interleaved with no mixing matrix
1020 convert
= av_audio_convert_alloc( SAMPLE_FMT_S16
, 1, codec_context
->sample_fmt
, 1, NULL
, 0 );
1021 mlt_properties_set_data( properties
, "audio_convert", convert
, 0, ( mlt_destructor
)av_audio_convert_free
, NULL
);
1025 // Check for audio buffer and create if necessary
1026 if ( audio_buffer
== NULL
)
1028 // Allocate the audio buffer
1029 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1031 // And store it on properties for reuse
1032 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1035 // Check for decoder buffer and create if necessary
1036 if ( decode_buffer
== NULL
)
1038 // Allocate the audio buffer
1039 decode_buffer
= av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1041 // And store it on properties for reuse
1042 mlt_properties_set_data( properties
, "decode_buffer", decode_buffer
, 0, ( mlt_destructor
)av_free
, NULL
);
1045 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1046 // Check for format converter buffer and create if necessary
1047 if ( resample
&& convert
&& convert_buffer
== NULL
)
1049 // Allocate the audio buffer
1050 convert_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1052 // And store it on properties for reuse
1053 mlt_properties_set_data( properties
, "convert_buffer", convert_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1057 // Seek if necessary
1058 if ( position
!= expected
)
1060 if ( position
+ 1 == expected
)
1062 // We're paused - silence required
1065 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
1067 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1068 ignore
= position
- expected
;
1070 else if ( position
< expected
|| position
- expected
>= 12 )
1072 // Set to the real timecode
1073 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
1076 // Clear the usage in the audio buffer
1081 // Get the audio if required
1087 av_init_packet( &pkt
);
1089 while( ret
>= 0 && !got_audio
)
1091 // Check if the buffer already contains the samples required
1092 if ( audio_used
>= *samples
&& ignore
== 0 )
1099 ret
= av_read_frame( context
, &pkt
);
1102 uint8_t *ptr
= pkt
.data
;
1104 // We only deal with audio from the selected audio_index
1105 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
1107 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
1110 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1111 ret
= avcodec_decode_audio2( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1113 ret
= avcodec_decode_audio( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1124 if ( data_size
> 0 )
1126 int src_stride
[6]= { av_get_bits_per_sample_format( codec_context
->sample_fmt
) / 8 };
1127 int dst_stride
[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16
) / 8 };
1131 int16_t *source
= decode_buffer
;
1132 int16_t *dest
= &audio_buffer
[ audio_used
* *channels
];
1133 int convert_samples
= data_size
/ src_stride
[0];
1135 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1138 const void *src_buf
[6] = { decode_buffer
};
1139 void *dst_buf
[6] = { convert_buffer
};
1140 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, convert_samples
);
1141 source
= convert_buffer
;
1144 audio_used
+= audio_resample( resample
, dest
, source
, convert_samples
/ codec_context
->channels
);
1148 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1151 const void *src_buf
[6] = { decode_buffer
};
1152 void *dst_buf
[6] = { &audio_buffer
[ audio_used
* *channels
] };
1153 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, data_size
/ src_stride
[0] );
1158 memcpy( &audio_buffer
[ audio_used
* *channels
], decode_buffer
, data_size
);
1160 audio_used
+= data_size
/ *channels
/ src_stride
[0];
1164 while ( ignore
&& audio_used
> *samples
)
1167 audio_used
-= *samples
;
1168 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
1172 // If we're behind, ignore this packet
1175 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
1176 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
1181 // We're finished with this packet regardless
1182 av_free_packet( &pkt
);
1185 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
1186 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1188 // Now handle the audio if we have enough
1189 if ( audio_used
>= *samples
)
1191 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
1192 audio_used
-= *samples
;
1193 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
1197 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
1200 // Store the number of audio samples still available
1201 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
1205 // Get silence and don't touch the context
1206 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
1209 // Regardless of speed (other than paused), we expect to get the next frame
1211 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
1216 /** Set up audio handling.
1219 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
1221 // Get the properties
1222 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1224 // Fetch the audio_context
1225 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1227 // Get the audio_index
1228 int index
= mlt_properties_get_int( properties
, "audio_index" );
1230 // Reopen the file if necessary
1231 if ( !context
&& index
> -1 )
1233 mlt_events_block( properties
, this );
1234 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1235 mlt_properties_get( properties
, "resource" ) );
1236 context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1237 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
1238 mlt_events_unblock( properties
, this );
1241 // Exception handling for audio_index
1242 if ( context
&& index
>= (int) context
->nb_streams
)
1244 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
; --index
);
1245 mlt_properties_set_int( properties
, "audio_index", index
);
1247 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
)
1250 mlt_properties_set_int( properties
, "audio_index", index
);
1253 // Update the audio properties if the index changed
1254 if ( index
> -1 && index
!= mlt_properties_get_int( properties
, "_audio_index" ) )
1256 mlt_properties_set_int( properties
, "_audio_index", index
);
1257 mlt_properties_set_data( properties
, "audio_codec", NULL
, 0, NULL
, NULL
);
1260 // Deal with audio context
1261 if ( context
!= NULL
&& index
> -1 )
1263 // Get the frame properties
1264 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1266 // Get the audio stream
1267 AVStream
*stream
= context
->streams
[ index
];
1269 // Get codec context
1270 AVCodecContext
*codec_context
= stream
->codec
;
1273 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1275 // Initialise the codec if necessary
1276 if ( codec
== NULL
)
1279 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1281 // If we don't have a codec and we can't initialise it, we can't do much more...
1283 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1285 // Now store the codec with its destructor
1286 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1291 // Remember that we can't use this later
1292 mlt_properties_set_int( properties
, "audio_index", -1 );
1298 // No codec, no show...
1299 if ( codec
&& index
> -1 )
1301 mlt_frame_push_audio( frame
, producer_get_audio
);
1302 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1303 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
1304 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
1309 /** Our get frame implementation.
1312 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1314 // Create an empty frame
1315 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1317 // Update timecode on the frame we're creating
1318 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1320 // Set the position of this producer
1321 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1324 producer_set_up_video( this, *frame
);
1327 producer_set_up_audio( this, *frame
);
1329 // Set the aspect_ratio
1330 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1332 // Calculate the next timecode
1333 mlt_producer_prepare_next( this );