1 /*
2  * producer_avformat.c -- avformat producer
3  * Copyright (C) 2003-2021 Meltytech, LLC
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L
21 #  undef _POSIX_C_SOURCE
22 #  define _POSIX_C_SOURCE 200809L
23 #endif
24 #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 500
25 #  undef _XOPEN_SOURCE
26 #  define _XOPEN_SOURCE 500
27 #endif
28 
29 #include "common.h"
30 
31 // MLT Header files
32 #include <framework/mlt_producer.h>
33 #include <framework/mlt_frame.h>
34 #include <framework/mlt_profile.h>
35 #include <framework/mlt_log.h>
36 #include <framework/mlt_deque.h>
37 #include <framework/mlt_factory.h>
38 #include <framework/mlt_cache.h>
39 #include <framework/mlt_slices.h>
40 
41 // ffmpeg Header files
42 #include <libavformat/avformat.h>
43 #include <libswscale/swscale.h>
44 #include <libavutil/samplefmt.h>
45 #include <libavutil/pixdesc.h>
46 #include <libavutil/dict.h>
47 #include <libavutil/opt.h>
48 #include <libavutil/channel_layout.h>
49 #include <libavutil/imgutils.h>
50 #include <libavutil/version.h>
51 
52 #define USE_HWACCEL 1
53 #if USE_HWACCEL
54 #include <libavutil/hwcontext.h>
55 #endif
56 
57 #ifdef AVFILTER
58 #include <libavfilter/avfilter.h>
59 #include <libavfilter/buffersink.h>
60 #include <libavfilter/buffersrc.h>
61 #endif
62 
63 // System header files
64 #include <stdlib.h>
65 #include <string.h>
66 #include <pthread.h>
67 #include <limits.h>
68 #include <math.h>
69 #include <wchar.h>
70 #include <stdatomic.h>
71 
72 #define POSITION_INITIAL (-2)
73 #define POSITION_INVALID (-1)
74 
75 #define MAX_AUDIO_STREAMS (32)
76 #define MAX_AUDIO_FRAME_SIZE (192000) // 1 second of 48khz 32bit audio
77 #define IMAGE_ALIGN (1)
78 #define VFR_THRESHOLD (3) // The minimum number of video frames with differing durations to be considered VFR.
79 
80 struct producer_avformat_s
81 {
82 	mlt_producer parent;
83 	AVFormatContext *dummy_context;
84 	AVFormatContext *audio_format;
85 	AVFormatContext *video_format;
86 	AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
87 	AVCodecContext *video_codec;
88 	AVFrame *video_frame;
89 	AVFrame *audio_frame;
90 	AVPacket pkt;
91 	mlt_position audio_expected;
92 	mlt_position video_expected;
93 	int audio_index;
94 	int video_index;
95 	int64_t first_pts;
96 	atomic_int_fast64_t last_position;
97 	int video_seekable;
98 	int seekable; /// This one is used for both audio and file level seekability.
99 	atomic_int_fast64_t current_position;
100 	mlt_position nonseek_position;
101 	atomic_int top_field_first;
102 	uint8_t *audio_buffer[ MAX_AUDIO_STREAMS ];
103 	int audio_buffer_size[ MAX_AUDIO_STREAMS ];
104 	uint8_t *decode_buffer[ MAX_AUDIO_STREAMS ];
105 	int audio_used[ MAX_AUDIO_STREAMS ];
106 	int audio_streams;
107 	int audio_max_stream;
108 	int total_channels;
109 	int max_channel;
110 	int max_frequency;
111 	unsigned int invalid_pts_counter;
112 	unsigned int invalid_dts_counter;
113 	mlt_cache image_cache;
114 	int yuv_colorspace, color_primaries, color_trc;
115 	int full_luma;
116 	pthread_mutex_t video_mutex;
117 	pthread_mutex_t audio_mutex;
118 	mlt_deque apackets;
119 	mlt_deque vpackets;
120 	pthread_mutex_t packets_mutex;
121 	pthread_mutex_t open_mutex;
122 	int is_mutex_init;
123 	AVRational video_time_base;
124 	mlt_frame last_good_frame; // for video error concealment
125 	int last_good_position;    // for video error concealment
126 #ifdef AVFILTER
127 	AVFilterGraph *vfilter_graph;
128 	AVFilterContext *vfilter_in;
129 	AVFilterContext* vfilter_out;
130 #endif
131 	int autorotate;
132 	int is_audio_synchronizing;
133 	int video_send_result;
134 #if USE_HWACCEL
135 	struct {
136 		int pix_fmt;
137 		int device_type;
138 		char device[128];
139 		AVBufferRef* device_ctx;
140 	} hwaccel;
141 #endif
142 };
143 typedef struct producer_avformat_s *producer_avformat;
144 
145 // Forward references.
146 static int list_components( char* file );
147 static int producer_open( producer_avformat self, mlt_profile profile, const char *URL, int take_lock, int test_open );
148 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
149 static void producer_avformat_close( producer_avformat );
150 static void producer_close( mlt_producer parent );
151 static void producer_set_up_video( producer_avformat self, mlt_frame frame );
152 static void producer_set_up_audio( producer_avformat self, mlt_frame frame );
153 static void apply_properties( void *obj, mlt_properties properties, int flags );
154 static int video_codec_init( producer_avformat self, int index, mlt_properties properties );
155 static void get_audio_streams_info( producer_avformat self );
156 static mlt_audio_format pick_audio_format( int sample_fmt );
157 static int pick_av_pixel_format( int *pix_fmt );
158 
159 /** Constructor for libavformat.
160 */
161 
producer_avformat_init(mlt_profile profile,const char * service,char * file)162 mlt_producer producer_avformat_init( mlt_profile profile, const char *service, char *file )
163 {
164 	if ( list_components( file ) )
165 		return NULL;
166 
167 	mlt_producer producer = NULL;
168 
169 	// Check that we have a non-NULL argument
170 	if ( file )
171 	{
172 		// Construct the producer
173 		producer_avformat self = calloc( 1, sizeof( struct producer_avformat_s ) );
174 		producer = calloc( 1, sizeof( struct mlt_producer_s ) );
175 
176 		// Initialise it
177 		if ( mlt_producer_init( producer, self ) == 0 )
178 		{
179 			self->parent = producer;
180 
181 			// Get the properties
182 			mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
183 
184 			// Set the resource property (required for all producers)
185 			mlt_properties_set( properties, "resource", file );
186 
187 			// Register transport implementation with the producer
188 			producer->close = (mlt_destructor) producer_close;
189 
190 			// Register our get_frame implementation
191 			producer->get_frame = producer_get_frame;
192 
193 			// Force the duration to be computed unless explicitly provided.
194 			mlt_properties_set_position( properties, "length", 0 );
195 			mlt_properties_set_position( properties, "out", 0 );
196 
197 			if ( strcmp( service, "avformat-novalidate" ) )
198 			{
199 				// Open the file
200 				if ( producer_open( self, profile, mlt_properties_get( properties, "resource" ), 1, 1 ) != 0 )
201 				{
202 					// Clean up
203 					mlt_producer_close( producer );
204 					producer = NULL;
205 					producer_avformat_close( self );
206 				}
207 				else if ( self->seekable )
208 				{
209 					// Close the file to release resources for large playlists - reopen later as needed
210 					if ( self->audio_format )
211 						avformat_close_input( &self->audio_format );
212 					if ( self->video_format )
213 						avformat_close_input( &self->video_format );
214 					self->audio_format = NULL;
215 					self->video_format = NULL;
216 				}
217 			}
218 			if ( producer )
219 			{
220 				// Default the user-selectable indices from the auto-detected indices
221 				mlt_properties_set_int( properties, "audio_index",  self->audio_index );
222 				mlt_properties_set_int( properties, "video_index",  self->video_index );
223 				mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", self, 0, (mlt_destructor) producer_avformat_close );
224 				mlt_properties_set_int( properties, "mute_on_pause",  1 );
225 			}
226 		}
227 	}
228 	return producer;
229 }
230 
list_components(char * file)231 int list_components( char* file )
232 {
233 	int skip = 0;
234 
235 	// Report information about available demuxers and codecs as YAML Tiny
236 	if ( file && strstr( file, "f-list" ) )
237 	{
238 		fprintf( stderr, "---\nformats:\n" );
239 		void *state = NULL;
240 		const AVInputFormat *format = NULL;
241 		while ((format = av_demuxer_iterate(&state))) {
242 			fprintf( stderr, "  - %s\n", format->name );
243 		}
244 		fprintf( stderr, "...\n" );
245 		skip = 1;
246 	}
247 	if ( file && strstr( file, "acodec-list" ) )
248 	{
249 		fprintf( stderr, "---\naudio_codecs:\n" );
250 		void *state = NULL;
251 		const AVCodec *codec = NULL;
252 		while ((codec = av_codec_iterate(&state))) {
253 			if ( codec->decode && codec->type == AVMEDIA_TYPE_AUDIO )
254 				fprintf( stderr, "  - %s\n", codec->name );
255 		}
256 		fprintf( stderr, "...\n" );
257 		skip = 1;
258 	}
259 	if ( file && strstr( file, "vcodec-list" ) )
260 	{
261 		fprintf( stderr, "---\nvideo_codecs:\n" );
262 		void *state = NULL;
263 		const AVCodec *codec = NULL;
264 		while ((codec = av_codec_iterate(&state))) {
265 			if ( codec->decode && codec->type == AVMEDIA_TYPE_VIDEO )
266 				fprintf( stderr, "  - %s\n", codec->name );
267 		}
268 		fprintf( stderr, "...\n" );
269 		skip = 1;
270 	}
271 
272 	return skip;
273 }
274 
first_video_index(producer_avformat self)275 static int first_video_index( producer_avformat self )
276 {
277 	AVFormatContext *context = self->video_format? self->video_format : self->audio_format;
278 	int result = -1; // not found
279 
280 	if ( context ) {
281 		unsigned int i;
282 		for ( i = 0; i < context->nb_streams; i++ ) {
283 			if ( context->streams[i]->codecpar &&
284 				 context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
285 				break;
286 		}
287 		if ( i < context->nb_streams ) {
288 			result = i;
289 	}
290 }
291 	return result;
292 }
293 
294 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
295 
296 #include <libavutil/spherical.h>
297 
get_projection(AVStream * st)298 static const char* get_projection(AVStream *st)
299 {
300 	const AVSphericalMapping *spherical = (const AVSphericalMapping*) av_stream_get_side_data(st, AV_PKT_DATA_SPHERICAL, NULL);
301 
302 	if (spherical)
303 		return av_spherical_projection_name(spherical->projection);
304 	return NULL;
305 }
306 
307 #endif
308 
309 #include <libavutil/display.h>
310 
get_rotation(AVStream * st)311 static double get_rotation(AVStream *st)
312 {
313 	AVDictionaryEntry *rotate_tag = av_dict_get( st->metadata, "rotate", NULL, 0 );
314 	uint8_t* displaymatrix = av_stream_get_side_data( st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
315 	double theta = 0;
316 
317 	if ( rotate_tag && *rotate_tag->value && strcmp( rotate_tag->value, "0" ) )
318 	{
319 		char *tail;
320 		theta = strtod( rotate_tag->value, &tail );
321 		if ( *tail )
322 			theta = 0;
323 	}
324 	if ( displaymatrix && !theta )
325 		theta = -av_display_rotation_get( (int32_t*) displaymatrix );
326 
327 	theta -= 360 * floor( theta/360 + 0.9/360 );
328 
329 	return theta;
330 }
331 
filter_restricted(const char * in)332 static char* filter_restricted( const char *in )
333 {
334 	if ( !in ) return NULL;
335 	size_t n = strlen( in );
336 	char *out = calloc( 1, n + 1 );
337 	char *p = out;
338 	mbstate_t mbs;
339 	memset( &mbs, 0, sizeof(mbs) );
340 	while ( *in )
341 	{
342 		wchar_t w;
343 		size_t c = mbrtowc( &w, in, n, &mbs );
344 		if ( c <= 0 || c > n ) break;
345 		n -= c;
346 		in += c;
347 		if ( w == 0x9 || w == 0xA || w == 0xD ||
348 				( w >= 0x20 && w <= 0xD7FF ) ||
349 				( w >= 0xE000 && w <= 0xFFFD ) ||
350 				( w >= 0x10000 && w <= 0x10FFFF ) )
351 		{
352 			mbstate_t ps;
353 			memset( &ps, 0, sizeof(ps) );
354 			c = wcrtomb( p, w, &ps );
355 			if ( c > 0 )
356 				p += c;
357 		}
358 	}
359 	return out;
360 }
361 
362 /** Find the default streams.
363 */
364 
find_default_streams(producer_avformat self)365 static mlt_properties find_default_streams( producer_avformat self )
366 {
367 	unsigned int i;
368 	char key[200];
369 	AVDictionaryEntry *tag = NULL;
370 	AVFormatContext *context = self->video_format;
371 	mlt_properties meta_media = MLT_PRODUCER_PROPERTIES( self->parent );
372 
373 	// Default to the first audio and video streams found
374 	self->audio_index = -1;
375 	int first_video_index = self->video_index = -1;
376 
377 	mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
378 
379 	// Allow for multiple audio and video streams in the file and select first of each (if available)
380 	for( i = 0; i < context->nb_streams; i++ )
381 	{
382 		// Get the codec context
383 		AVStream *stream = context->streams[ i ];
384 		if ( ! stream ) continue;
385 		AVCodecContext *codec_context = stream->codec;
386 		if ( ! codec_context ) continue;
387 		AVCodecParameters *codec_params = stream->codecpar;
388 		AVCodec *codec = avcodec_find_decoder( codec_params->codec_id );
389 		if ( ! codec ) continue;
390 
391 		snprintf( key, sizeof(key), "meta.media.%u.stream.type", i );
392 
393 		// Determine the type and obtain the first index of each type
394 		switch( codec_params->codec_type )
395 		{
396 			case AVMEDIA_TYPE_VIDEO:
397 				// Save the first video stream
398 				if ( first_video_index < 0 )
399 					first_video_index = i;
400 				// Only set the video stream if not album art
401 				if (self->video_index < 0 && !(context->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
402 					self->video_index = i;
403 				}
404 				mlt_properties_set( meta_media, key, "video" );
405 				snprintf( key, sizeof(key), "meta.media.%u.stream.frame_rate", i );
406 				double ffmpeg_fps = av_q2d( context->streams[ i ]->avg_frame_rate );
407 				mlt_properties_set_double( meta_media, key, ffmpeg_fps );
408 
409 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
410 				const char *projection = get_projection(context->streams[i]);
411 				if (projection) {
412 					snprintf(key, sizeof(key), "meta.media.%u.stream.projection", i);
413 					mlt_properties_set_string(meta_media, key, projection);
414 				}
415 #endif
416 				snprintf( key, sizeof(key), "meta.media.%u.stream.sample_aspect_ratio", i );
417 				mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
418 				snprintf( key, sizeof(key), "meta.media.%u.codec.width", i );
419 				mlt_properties_set_int( meta_media, key, codec_params->width );
420 				snprintf( key, sizeof(key), "meta.media.%u.codec.height", i );
421 				mlt_properties_set_int( meta_media, key, codec_params->height );
422 				snprintf( key, sizeof(key), "meta.media.%u.codec.rotate", i );
423 				mlt_properties_set_int( meta_media, key, get_rotation(context->streams[i]) );
424 				snprintf( key, sizeof(key), "meta.media.%u.codec.frame_rate", i );
425 				AVRational frame_rate = { codec_context->time_base.den, codec_context->time_base.num * codec_context->ticks_per_frame };
426 				mlt_properties_set_double( meta_media, key, av_q2d( frame_rate ) );
427 				snprintf( key, sizeof(key), "meta.media.%u.codec.pix_fmt", i );
428 				mlt_properties_set( meta_media, key, av_get_pix_fmt_name( codec_params->format ) );
429 				snprintf( key, sizeof(key), "meta.media.%u.codec.sample_aspect_ratio", i );
430 				mlt_properties_set_double( meta_media, key, av_q2d( codec_params->sample_aspect_ratio ) );
431 				snprintf( key, sizeof(key), "meta.media.%u.codec.colorspace", i );
432 				switch ( codec_params->color_space )
433 				{
434 				case AVCOL_SPC_SMPTE240M:
435 					mlt_properties_set_int( meta_media, key, 240 );
436 					break;
437 				case AVCOL_SPC_BT470BG:
438 				case AVCOL_SPC_SMPTE170M:
439 					mlt_properties_set_int( meta_media, key, 601 );
440 					break;
441 				case AVCOL_SPC_BT709:
442 					mlt_properties_set_int( meta_media, key, 709 );
443 					break;
444 				case AVCOL_SPC_UNSPECIFIED:
445 				case AVCOL_SPC_RESERVED:
446 					// This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
447 					mlt_properties_set_int( meta_media, key, codec_params->width * codec_params->height > 750000 ? 709 : 601 );
448 					break;
449 				default:
450 					mlt_properties_set_int( meta_media, key, codec_context->colorspace );
451 					break;
452 				}
453 				if ( codec_params->color_trc && codec_params->color_trc != AVCOL_TRC_UNSPECIFIED )
454 				{
455 					snprintf( key, sizeof(key), "meta.media.%u.codec.color_trc", i );
456 					mlt_properties_set_double( meta_media, key, codec_params->color_trc );
457 				}
458 				break;
459 			case AVMEDIA_TYPE_AUDIO:
460 				if ( !codec_params->channels )
461 					break;
462 				// Use first audio stream
463 				if ( self->audio_index < 0 && pick_audio_format( codec_params->format ) != mlt_audio_none )
464 					self->audio_index = i;
465 
466 				mlt_properties_set( meta_media, key, "audio" );
467 				snprintf( key, sizeof(key), "meta.media.%u.codec.sample_fmt", i );
468 				mlt_properties_set( meta_media, key, av_get_sample_fmt_name( codec_params->format ) );
469 				snprintf( key, sizeof(key), "meta.media.%u.codec.sample_rate", i );
470 				mlt_properties_set_int( meta_media, key, codec_params->sample_rate );
471 				snprintf( key, sizeof(key), "meta.media.%u.codec.channels", i );
472 				mlt_properties_set_int( meta_media, key, codec_params->channels );
473 				break;
474 			default:
475 				break;
476 		}
477 // 		snprintf( key, sizeof(key), "meta.media.%u.stream.time_base", i );
478 // 		mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
479 		snprintf( key, sizeof(key), "meta.media.%u.codec.name", i );
480 		mlt_properties_set( meta_media, key, codec->name );
481 		snprintf( key, sizeof(key), "meta.media.%u.codec.long_name", i );
482 		mlt_properties_set( meta_media, key, codec->long_name );
483 		snprintf( key, sizeof(key), "meta.media.%u.codec.bit_rate", i );
484 		mlt_properties_set_int64( meta_media, key, codec_params->bit_rate );
485 // 		snprintf( key, sizeof(key), "meta.media.%u.codec.time_base", i );
486 // 		mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
487 //		snprintf( key, sizeof(key), "meta.media.%u.codec.profile", i );
488 //		mlt_properties_set_int( meta_media, key, codec_context->profile );
489 //		snprintf( key, sizeof(key), "meta.media.%u.codec.level", i );
490 //		mlt_properties_set_int( meta_media, key, codec_context->level );
491 
492 		// Read Metadata
493 		while ( ( tag = av_dict_get( stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
494 		{
495 			if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
496 			{
497 				snprintf( key, sizeof(key), "meta.attr.%u.stream.%s.markup", i, tag->key );
498 				char* value = filter_restricted( tag->value );
499 				mlt_properties_set( meta_media, key, value );
500 				free( value );
501 			}
502 		}
503 	}
504 
505 	// Use the album art if that is all we have
506 	if (self->video_index < 0 && first_video_index >= 0)
507 		self->video_index = first_video_index;
508 
509 	while ( ( tag = av_dict_get( context->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
510 	{
511 		if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
512 		{
513 			snprintf( key, sizeof(key), "meta.attr.%s.markup", tag->key );
514 			char* value = filter_restricted( tag->value );
515 			mlt_properties_set( meta_media, key, value );
516 			free( value );
517 		}
518 	}
519 
520 	return meta_media;
521 }
522 
get_aspect_ratio(mlt_properties properties,AVStream * stream,AVCodecParameters * codec_params)523 static void get_aspect_ratio( mlt_properties properties, AVStream *stream, AVCodecParameters *codec_params )
524 {
525 	AVRational sar = stream->sample_aspect_ratio;
526 	if ( sar.num <= 0 || sar.den <= 0 )
527 		sar = codec_params->sample_aspect_ratio;
528 	if ( sar.num <= 0 || sar.den <= 0 )
529 		sar.num = sar.den = 1;
530 	mlt_properties_set_int( properties, "meta.media.sample_aspect_num", sar.num );
531 	mlt_properties_set_int( properties, "meta.media.sample_aspect_den", sar.den );
532 	mlt_properties_set_double( properties, "aspect_ratio", av_q2d( sar ) );
533 }
534 
parse_url(mlt_profile profile,const char * URL,AVInputFormat ** format,AVDictionary ** params)535 static char* parse_url( mlt_profile profile, const char* URL, AVInputFormat **format, AVDictionary **params )
536 {
537 	(void) profile; // unused
538 	if ( !URL ) return NULL;
539 
540 	char *protocol = strdup( URL );
541 	char *url = strchr( protocol, ':' );
542 
543 	// Truncate protocol string
544 	if (url && (url - protocol) > 1 && avio_check(URL, 0) < 0) { // if defined and not a drive letter
545 		url[0] = '\0';
546 		++url;
547 		mlt_log_debug( NULL, "%s: protocol=%s resource=%s\n", __FUNCTION__, protocol, url );
548 
549 		// Lookup the format
550 		*format = av_find_input_format( protocol );
551 	} else {
552 		url = protocol;
553 	}
554 
555 	// Eat the format designator
556 	char *result = url;
557 
558 	// support for legacy width and height parameters
559 	char *width = NULL;
560 	char *height = NULL;
561 
562 	// Parse out params
563 	char* query = strchr( url, '?' );
564 	if (*format) {
565 		// Query string delimiter is '?'
566 		url = ( query && query > url && query[-1] != '\\' ) ? query : NULL;
567 	} else {
568 		// Ignore unescaped question marks
569 		while ( query && query > url && query[-1] != '\\' ) {
570 			query = strchr( query + 1, '?' );
571 		}
572 		// Query string delimiter is '\?'
573 		url = ( query && query > url && query[-1] == '\\' ) ? query : NULL;
574 		if (url) url[-1] = '\0'; // null the backslash
575 	}
576 	while ( url )
577 	{
578 		url[0] = '\0';
579 		char *name = strdup( ++url );
580 		char *value = strchr( name, '=' );
581 		if ( !value )
582 			// Also accept : as delimiter for backwards compatibility.
583 			value = strchr( name, ':' );
584 		if ( value )
585 		{
586 			value[0] = '\0';
587 			value++;
588 			char *t = strchr( value, '&' );
589 			if ( t )
590 				t[0] = 0;
591 			// translate old parameters to new av_dict names
592 			if ( !strcmp( name, "frame_rate" ) )
593 				av_dict_set( params, "framerate", value, 0 );
594 			else if ( !strcmp( name, "pix_fmt" ) )
595 				av_dict_set( params, "pixel_format", value, 0 );
596 			else if ( !strcmp( name, "width" ) )
597 				width = strdup( value );
598 			else if ( !strcmp( name, "height" ) )
599 				height = strdup( value );
600 			else
601 				// generic demux/device option support
602 				av_dict_set( params, name, value, 0 );
603 		}
604 		free( name );
605 		url = strchr( url, '&' );
606 	}
607 	// continued support for legacy width and height parameters
608 	if ( width && height )
609 	{
610 		char *s = malloc( strlen( width ) + strlen( height ) + 2 );
611 		strcpy( s, width );
612 		strcat( s, "x");
613 		strcat( s, height );
614 		av_dict_set( params, "video_size", s, 0 );
615 		free( s );
616 	}
617 	free( width );
618 	free( height );
619 
620 	result = strdup(result);
621 	free( protocol );
622 	mlt_log_debug(NULL, "[producer avformat] %s filename = %s\n", __FUNCTION__, result);
623 	return result;
624 }
625 
pick_pix_fmt(enum AVPixelFormat pix_fmt)626 static enum AVPixelFormat pick_pix_fmt( enum AVPixelFormat pix_fmt )
627 {
628 	switch ( pix_fmt )
629 	{
630 	case AV_PIX_FMT_ARGB:
631 	case AV_PIX_FMT_RGBA:
632 	case AV_PIX_FMT_ABGR:
633 	case AV_PIX_FMT_BGRA:
634 		return AV_PIX_FMT_RGBA;
635 #if defined(FFUDIV)
636 	case AV_PIX_FMT_BAYER_RGGB16LE:
637 		return AV_PIX_FMT_RGB24;
638 #endif
639 #if USE_HWACCEL
640 	case AV_PIX_FMT_VAAPI:
641 	case AV_PIX_FMT_CUDA:
642 	case AV_PIX_FMT_VIDEOTOOLBOX:
643 	case AV_PIX_FMT_DXVA2_VLD:
644 	case AV_PIX_FMT_D3D11:
645 		return AV_PIX_FMT_YUV420P;
646 #endif
647 	default:
648 		return AV_PIX_FMT_YUV422P;
649 	}
650 }
651 
get_basic_info(producer_avformat self,mlt_profile profile,const char * filename)652 static int get_basic_info( producer_avformat self, mlt_profile profile, const char *filename )
653 {
654 	int error = 0;
655 
656 	// Get the properties
657 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( self->parent );
658 
659 	AVFormatContext *format = self->video_format;
660 
661 	// Get the duration
662 	if ( mlt_properties_get_position( properties, "length" ) <= 0 ||
663 		 mlt_properties_get_position( properties, "out" ) <= 0 )
664 	{
665 		if ( format->duration != AV_NOPTS_VALUE )
666 		{
667 			// This isn't going to be accurate for all formats
668 			// We will treat everything with the producer fps.
669 			mlt_position frames = ( mlt_position ) lrint( format->duration * mlt_profile_fps( profile ) / AV_TIME_BASE );
670 			if ( mlt_properties_get_position( properties, "out" ) <= 0 )
671 				mlt_properties_set_position( properties, "out", frames - 1 );
672 			if ( mlt_properties_get_position( properties, "length" ) <= 0 )
673 				mlt_properties_set_position( properties, "length", frames );
674 		}
675 		else if ( format->nb_streams > 0 && format->streams[0]->codecpar && format->streams[0]->codecpar->codec_id == AV_CODEC_ID_WEBP )
676 		{
677 			char *e = getenv( "MLT_DEFAULT_PRODUCER_LENGTH" );
678 			int p = e ? atoi( e ) : 15000;
679 			mlt_properties_set_int( properties, "out", MAX(0, p - 1) );
680 			mlt_properties_set_int( properties, "length", p );
681 		}
682 		else
683 		{
684 			// Set live sources to run forever
685 			if ( mlt_properties_get_position( properties, "length" ) <= 0 )
686 				mlt_properties_set_position( properties, "length", INT_MAX );
687 			if ( mlt_properties_get_position( properties, "out" ) <= 0 )
688 				mlt_properties_set_position( properties, "out", INT_MAX - 1 );
689 			mlt_properties_set( properties, "eof", "loop" );
690 		}
691 	}
692 
693 	// Check if we're seekable
694 	// avdevices are typically AVFMT_NOFILE and not seekable
695 	self->seekable = !format->iformat || !( format->iformat->flags & AVFMT_NOFILE );
696 	if ( format->pb )
697 	{
698 		// protocols can indicate if they support seeking
699 		self->seekable = format->pb->seekable;
700 	}
701 	if ( self->seekable )
702 	{
703 		// Do a more rigorous test of seekable on a disposable context
704 		if ( format->nb_streams > 0 && format->streams[0]->codecpar && format->streams[0]->codecpar->codec_id != AV_CODEC_ID_WEBP )
705 			self->seekable = av_seek_frame( format, -1, format->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
706 		mlt_properties_set_int( properties, "seekable", self->seekable );
707 		self->dummy_context = format;
708 		self->video_format = NULL;
709 		avformat_open_input( &self->video_format, filename, NULL, NULL );
710 		avformat_find_stream_info( self->video_format, NULL );
711 		format = self->video_format;
712 	}
713 	self->video_seekable = self->seekable;
714 
715 	// Fetch the width, height and aspect ratio
716 	if ( self->video_index != -1 )
717 	{
718 		AVCodecParameters *codec_params = format->streams[ self->video_index ]->codecpar;
719 		mlt_properties_set_int( properties, "width", codec_params->width );
720 		mlt_properties_set_int( properties, "height", codec_params->height );
721 		get_aspect_ratio( properties, format->streams[ self->video_index ], codec_params );
722 
723 		int pix_fmt = codec_params->format;
724 		pick_av_pixel_format( &pix_fmt );
725 		if ( pix_fmt != AV_PIX_FMT_NONE ) {
726 			// Verify that we can convert this to one of our image formats.
727 			struct SwsContext *context = sws_getContext( codec_params->width, codec_params->height, pix_fmt,
728 				codec_params->width, codec_params->height, pick_pix_fmt( codec_params->format ), SWS_BILINEAR, NULL, NULL, NULL);
729 			if ( context )
730 				sws_freeContext( context );
731 			else
732 				error = 1;
733 		} else {
734 			self->video_index = -1;
735 		}
736 	}
737 	return error;
738 }
739 
740 #ifdef AVFILTER
setup_video_filters(producer_avformat self)741 static int setup_video_filters( producer_avformat self )
742 {
743 	mlt_properties properties = MLT_PRODUCER_PROPERTIES(self->parent);
744 	AVFormatContext *format = self->video_format;
745 	AVStream* stream = format->streams[ self->video_index ];
746 	AVCodecParameters *codec_params = stream->codecpar;
747 
748 	self->vfilter_graph = avfilter_graph_alloc();
749 
750 	// From ffplay.c:configure_video_filters().
751 	char buffersrc_args[256];
752 	snprintf(buffersrc_args, sizeof(buffersrc_args),
753 		"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:frame_rate=%d/%d",
754 		codec_params->width, codec_params->height, codec_params->format,
755 		stream->time_base.num, stream->time_base.den,
756 		mlt_properties_get_int(properties, "meta.media.sample_aspect_num"),
757 		FFMAX(mlt_properties_get_int(properties, "meta.media.sample_aspect_den"), 1),
758 		stream->avg_frame_rate.num, FFMAX(stream->avg_frame_rate.den, 1));
759 
760 	int result = avfilter_graph_create_filter(&self->vfilter_in, avfilter_get_by_name("buffer"),
761 		"mlt_buffer", buffersrc_args, NULL, self->vfilter_graph);
762 
763 	if (result >= 0) {
764 		result = avfilter_graph_create_filter(&self->vfilter_out, avfilter_get_by_name("buffersink"),
765 			"mlt_buffersink", NULL, NULL, self->vfilter_graph);
766 
767 		if (result >= 0) {
768 			enum AVPixelFormat pix_fmts[] = { codec_params->format, AV_PIX_FMT_NONE };
769 			result = av_opt_set_int_list(self->vfilter_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
770 		}
771 	}
772 
773 	return result;
774 }
775 
insert_filter(AVFilterGraph * graph,AVFilterContext ** last_filter,const char * name,const char * args)776 static int insert_filter(AVFilterGraph *graph, AVFilterContext **last_filter, const char *name, const char *args)
777 {
778 	AVFilterContext *filt_ctx;
779 	int result = avfilter_graph_create_filter(&filt_ctx, avfilter_get_by_name(name),
780 		name, args, NULL, graph);
781 	if (result >= 0) {
782 		result = avfilter_link(filt_ctx, 0, *last_filter, 0);
783 		if (result >= 0)
784 			*last_filter = filt_ctx;
785 	}
786 	return result;
787 }
788 #endif
789 
790 /** Open the file.
791 */
792 
producer_open(producer_avformat self,mlt_profile profile,const char * URL,int take_lock,int test_open)793 static int producer_open(producer_avformat self, mlt_profile profile, const char *URL, int take_lock, int test_open )
794 {
795 	// Return an error code (0 == no error)
796 	int error = 0;
797 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( self->parent );
798 
799 	if ( !self->is_mutex_init )
800 	{
801 		pthread_mutexattr_t attr;
802 		pthread_mutexattr_init(&attr);
803 		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
804 		pthread_mutex_init( &self->audio_mutex, &attr );
805 		pthread_mutex_init( &self->video_mutex, &attr );
806 		pthread_mutex_init( &self->packets_mutex, &attr );
807 		pthread_mutex_init( &self->open_mutex, &attr );
808 		self->is_mutex_init = 1;
809 	}
810 
811 	// Lock the service
812 	if ( take_lock )
813 	{
814 		pthread_mutex_lock( &self->audio_mutex );
815 		pthread_mutex_lock( &self->video_mutex );
816 	}
817 	mlt_events_block( properties, self->parent );
818 
819 	// Parse URL
820 	AVInputFormat *format = NULL;
821 	AVDictionary *params = NULL;
822 	char *filename = parse_url( profile, URL, &format, &params );
823 
824 	// Now attempt to open the file or device with filename
825 	error = avformat_open_input( &self->video_format, filename, format, &params ) < 0;
826 	if ( error )
827 		// If the URL is a network stream URL, then we probably need to open with full URL
828 		error = avformat_open_input( &self->video_format, URL, format, &params ) < 0;
829 
830 	// Set MLT properties onto video AVFormatContext
831 	if ( !error && self->video_format )
832 	{
833 		apply_properties( self->video_format, properties, AV_OPT_FLAG_DECODING_PARAM );
834 		if ( self->video_format->iformat && self->video_format->iformat->priv_class && self->video_format->priv_data )
835 			apply_properties( self->video_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
836 	}
837 
838 	// If successful, then try to get additional info
839 	if ( !error && self->video_format )
840 	{
841 		// Get the stream info
842 		error = avformat_find_stream_info( self->video_format, NULL ) < 0;
843 
844 		// Continue if no error
845 		if ( !error && self->video_format )
846 		{
847 			// Find default audio and video streams
848 			find_default_streams( self );
849 			error = get_basic_info( self, profile, filename );
850 
851 			// Initialize position info
852 			self->first_pts = AV_NOPTS_VALUE;
853 			self->last_position = POSITION_INITIAL;
854 
855 #if USE_HWACCEL
856 			AVDictionaryEntry *hwaccel = av_dict_get( params, "hwaccel", NULL, 0 );
857 			AVDictionaryEntry *hwaccel_device = av_dict_get( params, "hwaccel_device", NULL, 0 );
858 
859 			if ( hwaccel && hwaccel->value )
860 			{
861 				// Leaving `device=NULL` will cause query string parameter `hwaccel_device` to be ignored
862 				char *device = NULL;
863 				if ( !strcmp( hwaccel->value, "vaapi" ) )
864 				{
865 					self->hwaccel.pix_fmt = AV_PIX_FMT_VAAPI;
866 					self->hwaccel.device_type = AV_HWDEVICE_TYPE_VAAPI;
867 					device = "/dev/dri/renderD128";
868 				}
869 				else if ( !strcmp( hwaccel->value, "cuda" ) )
870 				{
871 					self->hwaccel.pix_fmt = AV_PIX_FMT_CUDA;
872 					self->hwaccel.device_type = AV_HWDEVICE_TYPE_CUDA;
873 					device = "0";
874 				}
875 				else if ( !strcmp( hwaccel->value, "videotoolbox" ) )
876 				{
877 					self->hwaccel.pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX;
878 					self->hwaccel.device_type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
879 				}
880 				else if ( !strcmp( hwaccel->value, "d3d11va" ) )
881 				{
882 					self->hwaccel.pix_fmt = AV_PIX_FMT_D3D11;
883 					self->hwaccel.device_type = AV_HWDEVICE_TYPE_D3D11VA;
884 					device = "0";
885 				}
886 				else if ( !strcmp( hwaccel->value, "dxva2" ) )
887 				{
888 					self->hwaccel.pix_fmt = AV_PIX_FMT_DXVA2_VLD;
889 					self->hwaccel.device_type = AV_HWDEVICE_TYPE_DXVA2;
890 					device = "0";
891 				}
892 				else
893 				{
894 					// TODO: init other hardware types
895 				}
896 
897 				if (device) {
898 					if (hwaccel_device && hwaccel_device->value)
899 						device = hwaccel_device->value;
900 					memcpy(self->hwaccel.device, device, strlen(device));
901 				}
902 			}
903 #endif
904 
905 			if ( !self->audio_format )
906 			{
907 				// We're going to cheat here - for seekable A/V files, we will have separate contexts
908 				// to support independent seeking of audio from video.
909 				// TODO: Is this really necessary?
910 				if ( self->audio_index != -1 && self->video_index != -1 )
911 				{
912 					if ( self->seekable )
913 					{
914 						// And open again for our audio context
915 						avformat_open_input( &self->audio_format, filename, NULL, NULL );
916 						apply_properties( self->audio_format, properties, AV_OPT_FLAG_DECODING_PARAM );
917 						if ( self->audio_format->iformat && self->audio_format->iformat->priv_class && self->audio_format->priv_data )
918 							apply_properties( self->audio_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
919 						avformat_find_stream_info( self->audio_format, NULL );
920 					}
921 					else
922 					{
923 						self->audio_format = self->video_format;
924 					}
925 				}
926 				else if ( self->audio_index != -1 )
927 				{
928 					// We only have an audio context
929 					self->audio_format = self->video_format;
930 					self->video_format = NULL;
931 				}
932 				else if ( self->video_index == -1 )
933 				{
934 					// Something has gone wrong
935 					error = -1;
936 				}
937 				if ( self->audio_format && !self->audio_streams )
938 					get_audio_streams_info( self );
939 
940 #ifdef AVFILTER
941 				// Setup autorotate filters.
942 				if (self->video_index != -1) {
943 					self->autorotate = !mlt_properties_get(properties, "autorotate") || mlt_properties_get_int(properties, "autorotate");
944 					if (!test_open && self->autorotate && !self->vfilter_graph) {
945 						double theta  = get_rotation(self->video_format->streams[self->video_index]);
946 
947 						if (fabs(theta - 90) < 1.0) {
948 							error = ( setup_video_filters(self) < 0 );
949 							AVFilterContext *last_filter = self->vfilter_out;
950 							if (!error) error = ( insert_filter(self->vfilter_graph, &last_filter, "transpose", "clock") < 0 );
951 							if (!error) error = ( avfilter_link(self->vfilter_in, 0, last_filter, 0) < 0 );
952 							if (!error) error = ( avfilter_graph_config(self->vfilter_graph, NULL) < 0 );
953 						} else if (fabs(theta - 180) < 1.0) {
954 							error = ( setup_video_filters(self) < 0 );
955 							AVFilterContext *last_filter = self->vfilter_out;
956 							if (!error) error = ( insert_filter(self->vfilter_graph, &last_filter, "hflip", NULL) < 0 );
957 							if (!error) error = ( insert_filter(self->vfilter_graph, &last_filter, "vflip", NULL) < 0 );
958 							if (!error) error = ( avfilter_link(self->vfilter_in, 0, last_filter, 0) < 0 );
959 							if (!error) error = ( avfilter_graph_config(self->vfilter_graph, NULL) < 0 );
960 						} else if (fabs(theta - 270) < 1.0) {
961 							error = ( setup_video_filters(self) < 0 );
962 							AVFilterContext *last_filter = self->vfilter_out;
963 							if (!error) error = ( insert_filter(self->vfilter_graph, &last_filter, "transpose", "cclock") < 0 );
964 							if (!error) error = ( avfilter_link(self->vfilter_in, 0, last_filter, 0) < 0 );
965 							if (!error) error = ( avfilter_graph_config(self->vfilter_graph, NULL) < 0 );
966 						}
967 					}
968 				}
969 #endif
970 			}
971 		}
972 	}
973 	av_dict_free( &params );
974 	free( filename );
975 	if ( !error )
976 	{
977 		self->apackets = mlt_deque_init();
978 		self->vpackets = mlt_deque_init();
979 	}
980 
981 	if ( self->dummy_context )
982 	{
983 		pthread_mutex_lock( &self->open_mutex );
984 		avformat_close_input( &self->dummy_context );
985 		self->dummy_context = NULL;
986 		pthread_mutex_unlock( &self->open_mutex );
987 	}
988 
989 	// Unlock the service
990 	if ( take_lock )
991 	{
992 		pthread_mutex_unlock( &self->audio_mutex );
993 		pthread_mutex_unlock( &self->video_mutex );
994 	}
995 	mlt_events_unblock( properties, self->parent );
996 
997 	return error;
998 }
999 
prepare_reopen(producer_avformat self)1000 static void prepare_reopen( producer_avformat self )
1001 {
1002 	mlt_service_lock( MLT_PRODUCER_SERVICE( self->parent ) );
1003 	pthread_mutex_lock( &self->audio_mutex );
1004 	pthread_mutex_lock( &self->open_mutex );
1005 
1006 	int i;
1007 	for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
1008 	{
1009 		mlt_pool_release( self->audio_buffer[i] );
1010 		self->audio_buffer[i] = NULL;
1011 		av_free( self->decode_buffer[i] );
1012 		self->decode_buffer[i] = NULL;
1013 		if ( self->audio_codec[i] )
1014 			avcodec_close( self->audio_codec[i] );
1015 		self->audio_codec[i] = NULL;
1016 	}
1017 	if ( self->video_codec )
1018 		avcodec_close( self->video_codec );
1019 	self->video_codec = NULL;
1020 	av_frame_unref( self->video_frame );
1021 #if USE_HWACCEL
1022 	av_buffer_unref( &self->hwaccel.device_ctx );
1023 	self->hwaccel.device_ctx = NULL;
1024 #endif
1025 	if ( self->seekable && self->audio_format )
1026 		avformat_close_input( &self->audio_format );
1027 	if ( self->video_format )
1028 		avformat_close_input( &self->video_format );
1029 	self->audio_format = NULL;
1030 	self->video_format = NULL;
1031 #ifdef AVFILTER
1032 	avfilter_graph_free( &self->vfilter_graph );
1033 #endif
1034 	pthread_mutex_unlock( &self->open_mutex );
1035 
1036 	// Cleanup the packet queues
1037 	AVPacket *pkt;
1038 	if ( self->apackets )
1039 	{
1040 		while ( ( pkt = mlt_deque_pop_back( self->apackets ) ) )
1041 		{
1042 			av_packet_unref( pkt );
1043 			free( pkt );
1044 		}
1045 		mlt_deque_close( self->apackets );
1046 		self->apackets = NULL;
1047 	}
1048 	if ( self->vpackets )
1049 	{
1050 		while ( ( pkt = mlt_deque_pop_back( self->vpackets ) ) )
1051 		{
1052 			av_packet_unref( pkt );
1053 			free( pkt );
1054 		}
1055 		mlt_deque_close( self->vpackets );
1056 		self->vpackets = NULL;
1057 	}
1058 	pthread_mutex_unlock( &self->audio_mutex );
1059 	mlt_service_unlock( MLT_PRODUCER_SERVICE( self->parent ) );
1060 }
1061 
best_pts(producer_avformat self,int64_t pts,int64_t dts)1062 static int64_t best_pts( producer_avformat self, int64_t pts, int64_t dts )
1063 {
1064 	self->invalid_pts_counter += pts == AV_NOPTS_VALUE;
1065 	self->invalid_dts_counter += dts == AV_NOPTS_VALUE;
1066 	if ( ( self->invalid_pts_counter <= self->invalid_dts_counter
1067 		   || dts == AV_NOPTS_VALUE ) && pts != AV_NOPTS_VALUE )
1068 		return pts;
1069 	else
1070 		return dts;
1071 }
1072 
find_first_pts(producer_avformat self,int video_index)1073 static void find_first_pts( producer_avformat self, int video_index )
1074 {
1075 	// find initial PTS
1076 	AVFormatContext *context = self->video_format? self->video_format : self->audio_format;
1077 	int ret = 0;
1078 	int pkt_countdown = 500; // check max 500 packets for first video keyframe PTS
1079 	int vfr_countdown = 20;  // check max 20 video frames for VFR
1080 	int vfr_counter   = 0;   // counts the number of frame duration changes
1081 	AVPacket pkt;
1082 	int64_t prev_pkt_duration = AV_NOPTS_VALUE;
1083 
1084 	av_init_packet( &pkt );
1085 	while ( ret >= 0 && pkt_countdown-- > 0 &&
1086 	      ( self->first_pts == AV_NOPTS_VALUE || ( vfr_counter < VFR_THRESHOLD && vfr_countdown > 0 ) ) )
1087 	{
1088 		ret = av_read_frame( context, &pkt );
1089 		if ( ret >= 0 && pkt.stream_index == video_index )
1090 		{
1091 			// Variable frame rate check
1092 			if ( pkt.duration != AV_NOPTS_VALUE && pkt.duration != prev_pkt_duration ) {
1093 				mlt_log_verbose( MLT_PRODUCER_SERVICE(self->parent), "checking VFR: pkt.duration %"PRId64"\n", pkt.duration );
1094 				if ( prev_pkt_duration != AV_NOPTS_VALUE )
1095 					++vfr_counter;
1096 			}
1097 			prev_pkt_duration = pkt.duration;
1098 			vfr_countdown--;
1099 
1100 			// Finding PTS of first video key frame
1101 			if ( ( pkt.flags & AV_PKT_FLAG_KEY ) && self->first_pts == AV_NOPTS_VALUE )
1102 			{
1103 				mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent),
1104 					"first_pts %"PRId64" dts %"PRId64" pts_dts_delta %d\n",
1105 					pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) );
1106 				if ( pkt.dts != AV_NOPTS_VALUE && pkt.dts < 0 )
1107 					// Decoding Time Stamps with negative values are reported by ffmpeg code for
1108 					// (at least) MP4 files containing h.264 video using b-frames.
1109 					// For reasons not understood yet, the first PTS computed then is that of the
1110 					// third frame, causing MLT to display the third frame as if it was the first.
1111 					// This if-clause is meant to catch and work around this issue - if there is
1112 					// a valid, but negative DTS value, we just guess that the first valid
1113 					// Presentation Time Stamp is == 0.
1114 					self->first_pts = 0;
1115 				else
1116 					self->first_pts = best_pts( self, pkt.pts, pkt.dts );
1117 			}
1118 		}
1119 		av_packet_unref( &pkt );
1120 	}
1121 	if ( vfr_counter >= VFR_THRESHOLD )
1122 		mlt_properties_set_int( MLT_PRODUCER_PROPERTIES(self->parent), "meta.media.variable_frame_rate", 1 );
1123 	av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
1124 }
1125 
seek_video(producer_avformat self,mlt_position position,int64_t req_position,int preseek)1126 static int seek_video( producer_avformat self, mlt_position position,
1127 	int64_t req_position, int preseek )
1128 {
1129 	mlt_producer producer = self->parent;
1130         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1131 	int paused = 0;
1132 	int seek_threshold = mlt_properties_get_int( properties, "seek_threshold" );
1133 	if ( seek_threshold <= 0 ) seek_threshold = 12;
1134 
1135 	pthread_mutex_lock( &self->packets_mutex );
1136 
1137 	if ( self->video_seekable && ( position != self->video_expected || self->last_position < 0 ) )
1138 	{
1139 
1140 		// Fetch the video format context
1141 		AVFormatContext *context = self->video_format;
1142 
1143 		// Get the video stream
1144 		AVStream *stream = context->streams[ self->video_index ];
1145 
1146 		// Get codec context
1147 		AVCodecContext *codec_context = stream->codec;
1148 
1149 		// We may want to use the source fps if available
1150 		double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1151 			mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1152 
1153 		if ( self->first_pts == AV_NOPTS_VALUE && self->last_position == POSITION_INITIAL )
1154 			find_first_pts( self, self->video_index );
1155 
1156 		if ( self->video_frame && position + 1 == self->video_expected )
1157 		{
1158 			// We're paused - use last image
1159 			paused = 1;
1160 		}
1161 		else if ( position < self->video_expected || position - self->video_expected >= seek_threshold || self->last_position < 0 )
1162 		{
1163 			// Calculate the timestamp for the requested frame
1164 			int64_t timestamp = req_position / ( av_q2d( self->video_time_base ) * source_fps );
1165 			if ( req_position <= 0 )
1166 				timestamp = 0;
1167 			else if ( self->first_pts != AV_NOPTS_VALUE )
1168 				timestamp += self->first_pts;
1169 			else if ( context->start_time != AV_NOPTS_VALUE )
1170 				timestamp += context->start_time;
1171 			if ( preseek && av_q2d( self->video_time_base ) != 0 )
1172 				timestamp -= 2 / av_q2d( self->video_time_base );
1173 			if ( timestamp < 0 )
1174 				timestamp = 0;
1175 			mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %"PRId64" position " MLT_POSITION_FMT " expected "MLT_POSITION_FMT" last_pos %"PRId64"\n",
1176 				timestamp, position, self->video_expected, self->last_position );
1177 
1178 			// Seek to the timestamp
1179 			codec_context->skip_loop_filter = AVDISCARD_NONREF;
1180 			av_seek_frame( context, self->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
1181 
1182 			// flush any pictures still in decode buffer
1183 			avcodec_flush_buffers( codec_context );
1184 			self->video_send_result = 0;
1185 
1186 			// Remove the cached info relating to the previous position
1187 			self->current_position = POSITION_INVALID;
1188 			self->last_position = POSITION_INVALID;
1189 			av_frame_unref(self->video_frame);
1190 		}
1191 	}
1192 	pthread_mutex_unlock( &self->packets_mutex );
1193 	return paused;
1194 }
1195 
1196 /** Convert a frame position to a time code.
1197 */
1198 
producer_time_of_frame(mlt_producer producer,mlt_position position)1199 static double producer_time_of_frame( mlt_producer producer, mlt_position position )
1200 {
1201 	return ( double )position / mlt_producer_get_fps( producer );
1202 }
1203 
1204 // Collect information about all audio streams
1205 
get_audio_streams_info(producer_avformat self)1206 static void get_audio_streams_info( producer_avformat self )
1207 {
1208 	// Fetch the audio format context
1209 	AVFormatContext *context = self->audio_format;
1210 	unsigned int i;
1211 
1212 	for ( i = 0;
1213 		  i < context->nb_streams;
1214 		  i++ )
1215 	{
1216 		if ( context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
1217 		{
1218 			AVCodecParameters *codec_params = context->streams[i]->codecpar;
1219 			AVCodec *codec = avcodec_find_decoder( codec_params->codec_id );
1220 
1221 			// If we don't have a codec and we can't initialise it, we can't do much more...
1222 			pthread_mutex_lock( &self->open_mutex );
1223 			if ( codec && avcodec_open2( context->streams[i]->codec, codec, NULL ) >= 0 )
1224 			{
1225 				self->audio_streams++;
1226 				self->audio_max_stream = i;
1227 				self->total_channels += codec_params->channels;
1228 				if ( codec_params->channels > self->max_channel )
1229 					self->max_channel = codec_params->channels;
1230 				if ( codec_params->sample_rate > self->max_frequency )
1231 					self->max_frequency = codec_params->sample_rate;
1232 				avcodec_close( context->streams[i]->codec );
1233 			}
1234 			pthread_mutex_unlock( &self->open_mutex );
1235 		}
1236 	}
1237 	mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
1238 		self->audio_streams, self->audio_max_stream, self->total_channels, self->max_channel );
1239 }
1240 
pick_image_format(enum AVPixelFormat pix_fmt)1241 static mlt_image_format pick_image_format( enum AVPixelFormat pix_fmt )
1242 {
1243 	switch ( pix_fmt )
1244 	{
1245 	case AV_PIX_FMT_ARGB:
1246 	case AV_PIX_FMT_RGBA:
1247 	case AV_PIX_FMT_ABGR:
1248 	case AV_PIX_FMT_BGRA:
1249 		return mlt_image_rgba;
1250 	case AV_PIX_FMT_YUV420P:
1251 	case AV_PIX_FMT_YUVJ420P:
1252 	case AV_PIX_FMT_YUVA420P:
1253 		return mlt_image_yuv420p;
1254 	case AV_PIX_FMT_RGB24:
1255 	case AV_PIX_FMT_BGR24:
1256 	case AV_PIX_FMT_GRAY8:
1257 	case AV_PIX_FMT_MONOWHITE:
1258 	case AV_PIX_FMT_MONOBLACK:
1259 	case AV_PIX_FMT_RGB8:
1260 	case AV_PIX_FMT_BGR8:
1261 #if defined(FFUDIV)
1262 	case AV_PIX_FMT_BAYER_RGGB16LE:
1263 		return mlt_image_rgb;
1264 #endif
1265 	default:
1266 		return mlt_image_yuv422;
1267 	}
1268 }
1269 
pick_audio_format(int sample_fmt)1270 static mlt_audio_format pick_audio_format( int sample_fmt )
1271 {
1272 	switch ( sample_fmt )
1273 	{
1274 	// interleaved
1275 	case AV_SAMPLE_FMT_U8:
1276 		return mlt_audio_u8;
1277 	case AV_SAMPLE_FMT_S16:
1278 		return mlt_audio_s16;
1279 	case AV_SAMPLE_FMT_S32:
1280 		return mlt_audio_s32le;
1281 	case AV_SAMPLE_FMT_FLT:
1282 		return mlt_audio_f32le;
1283 	// planar - this producer converts planar to interleaved
1284 	case AV_SAMPLE_FMT_U8P:
1285 		return mlt_audio_u8;
1286 	case AV_SAMPLE_FMT_S16P:
1287 		return mlt_audio_s16;
1288 	case AV_SAMPLE_FMT_S32P:
1289 		return mlt_audio_s32le;
1290 	case AV_SAMPLE_FMT_FLTP:
1291 		return mlt_audio_f32le;
1292 	default:
1293 		return mlt_audio_none;
1294 	}
1295 }
1296 
1297 /**
1298  * Handle deprecated pixel format (JPEG range in YUV420P for example).
1299  *
1300  * Replace pix_fmt with the official pixel format to use.
1301  * @return 0 if no pix_fmt replacement, 1 otherwise
1302  */
pick_av_pixel_format(int * pix_fmt)1303 static int pick_av_pixel_format( int *pix_fmt )
1304 {
1305 #if defined(FFUDIV)
1306 	switch (*pix_fmt)
1307 	{
1308 	case AV_PIX_FMT_YUVJ420P:
1309 		*pix_fmt = AV_PIX_FMT_YUV420P;
1310 		return 1;
1311 	case AV_PIX_FMT_YUVJ411P:
1312 		*pix_fmt = AV_PIX_FMT_YUV411P;
1313 		return 1;
1314 	case AV_PIX_FMT_YUVJ422P:
1315 		*pix_fmt = AV_PIX_FMT_YUV422P;
1316 		return 1;
1317 	case AV_PIX_FMT_YUVJ444P:
1318 		*pix_fmt = AV_PIX_FMT_YUV444P;
1319 		return 1;
1320 	case AV_PIX_FMT_YUVJ440P:
1321 		*pix_fmt = AV_PIX_FMT_YUV440P;
1322 		return 1;
1323 	}
1324 #endif
1325 	return 0;
1326 }
1327 
1328 #if defined(FFUDIV) && (LIBSWSCALE_VERSION_INT >= ((3<<16)+(1<<8)+101))
1329 struct sliced_pix_fmt_conv_t
1330 {
1331 	int width, height, slice_w;
1332 	AVFrame *frame;
1333 	uint8_t *out_data[4];
1334 	int out_stride[4];
1335 	enum AVPixelFormat src_format, dst_format;
1336 	const AVPixFmtDescriptor *src_desc, *dst_desc;
1337 	int flags, src_colorspace, dst_colorspace, src_full_range, dst_full_range;
1338 };
1339 
sliced_h_pix_fmt_conv_proc(int id,int idx,int jobs,void * cookie)1340 static int sliced_h_pix_fmt_conv_proc( int id, int idx, int jobs, void* cookie )
1341 {
1342 	uint8_t *out[4];
1343 	const uint8_t *in[4];
1344 	int in_stride[4], out_stride[4];
1345 	int src_v_chr_pos = -513, dst_v_chr_pos = -513, ret, i, slice_x, slice_w, h, mul, field, slices, interlaced = 0;
1346 
1347 	struct SwsContext *sws;
1348 	struct sliced_pix_fmt_conv_t* ctx = ( struct sliced_pix_fmt_conv_t* )cookie;
1349 
1350 	interlaced = ctx->frame->interlaced_frame;
1351 	field = ( interlaced ) ? ( idx & 1 ) : 0;
1352 	idx = ( interlaced ) ? ( idx / 2 ) : idx;
1353 	slices = ( interlaced ) ? ( jobs / 2 ) : jobs;
1354 	mul = ( interlaced ) ? 2 : 1;
1355 	h = ctx->height >> !!interlaced;
1356 	slice_w = ctx->slice_w;
1357 	slice_x = slice_w * idx;
1358 	slice_w = FFMIN( slice_w, ctx->width - slice_x );
1359 
1360 	if ( AV_PIX_FMT_YUV420P == ctx->src_format )
1361 		src_v_chr_pos = ( !interlaced ) ? 128 : ( !field ) ? 64 : 192;
1362 
1363 	if ( AV_PIX_FMT_YUV420P == ctx->dst_format )
1364 		dst_v_chr_pos = ( !interlaced ) ? 128 : ( !field ) ? 64 : 192;
1365 
1366 	mlt_log_debug( NULL, "%s:%d: [id=%d, idx=%d, jobs=%d], interlaced=%d, field=%d, slices=%d, mul=%d, h=%d, slice_w=%d, slice_x=%d ctx->src_desc=[log2_chroma_h=%d, log2_chroma_w=%d], src_v_chr_pos=%d, dst_v_chr_pos=%d\n",
1367 		__FUNCTION__, __LINE__, id, idx, jobs, interlaced, field, slices, mul, h, slice_w, slice_x, ctx->src_desc->log2_chroma_h, ctx->src_desc->log2_chroma_w, src_v_chr_pos, dst_v_chr_pos );
1368 
1369 	if ( slice_w <= 0 )
1370 		return 0;
1371 
1372 	sws = sws_alloc_context();
1373 
1374 	av_opt_set_int( sws, "srcw", slice_w, 0 );
1375 	av_opt_set_int( sws, "srch", h, 0 );
1376 	av_opt_set_int( sws, "src_format", ctx->src_format, 0 );
1377 	av_opt_set_int( sws, "dstw", slice_w, 0 );
1378 	av_opt_set_int( sws, "dsth", h, 0 );
1379 	av_opt_set_int( sws, "dst_format", ctx->dst_format, 0 );
1380 	av_opt_set_int( sws, "sws_flags", ctx->flags, 0 );
1381 
1382 	av_opt_set_int( sws, "src_h_chr_pos", -513, 0 );
1383 	av_opt_set_int( sws, "src_v_chr_pos", src_v_chr_pos, 0 );
1384 	av_opt_set_int( sws, "dst_h_chr_pos", -513, 0 );
1385 	av_opt_set_int( sws, "dst_v_chr_pos", dst_v_chr_pos, 0 );
1386 
1387 	if ( ( ret = sws_init_context( sws, NULL, NULL ) ) < 0 )
1388 	{
1389 		mlt_log_error( NULL, "%s:%d: sws_init_context failed, ret=%d\n", __FUNCTION__, __LINE__, ret );
1390 		sws_freeContext( sws );
1391 		return 0;
1392 	}
1393 
1394 	mlt_set_luma_transfer( sws, ctx->src_colorspace, ctx->dst_colorspace, ctx->src_full_range, ctx->dst_full_range );
1395 
1396 #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(55, 0, 100)
1397 #define PIX_DESC_BPP(DESC) (DESC.step_minus1 + 1)
1398 #else
1399 #define PIX_DESC_BPP(DESC) (DESC.step)
1400 #endif
1401 
1402 	for( i = 0; i < 4; i++ )
1403 	{
1404 		int in_offset = (AV_PIX_FMT_FLAG_PLANAR & ctx->src_desc->flags)
1405 			? ( ( 1 == i || 2 == i ) ? ( slice_x >> ctx->src_desc->log2_chroma_w ) : slice_x )
1406 			: ( ( 0 == i ) ? slice_x : 0 );
1407 
1408 		int out_offset = (AV_PIX_FMT_FLAG_PLANAR & ctx->dst_desc->flags)
1409 			? ( ( 1 == i || 2 == i ) ? ( slice_x >> ctx->dst_desc->log2_chroma_w ) : slice_x )
1410 			: ( ( 0 == i ) ? slice_x : 0 );
1411 
1412 		in_offset *= PIX_DESC_BPP(ctx->src_desc->comp[i]);
1413 		out_offset *= PIX_DESC_BPP(ctx->dst_desc->comp[i]);
1414 
1415 		in_stride[i]  = ctx->frame->linesize[i] * mul;
1416 		out_stride[i] = ctx->out_stride[i] * mul;
1417 
1418 		in[i] =  ctx->frame->data[i] + ctx->frame->linesize[i] * field + in_offset;
1419 		out[i] = ctx->out_data[i] + ctx->out_stride[i] * field + out_offset;
1420 	}
1421 
1422 	sws_scale( sws, in, in_stride, 0, h, out, out_stride );
1423 
1424 	sws_freeContext( sws );
1425 
1426 	return 0;
1427 }
1428 #endif
1429 
1430 // returns resulting YUV colorspace
convert_image(producer_avformat self,AVFrame * frame,uint8_t * buffer,int pix_fmt,mlt_image_format * format,int width,int height,uint8_t ** alpha)1431 static int convert_image( producer_avformat self, AVFrame *frame, uint8_t *buffer, int pix_fmt,
1432 	mlt_image_format *format, int width, int height, uint8_t **alpha )
1433 {
1434 	mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self->parent ) );
1435 	int result = self->yuv_colorspace;
1436 
1437 	mlt_log_timings_begin();
1438 
1439 	mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "%s @ %dx%d space %d->%d\n",
1440 		mlt_image_format_name( *format ),
1441 		width, height, self->yuv_colorspace, profile->colorspace );
1442 
1443 	// extract alpha from planar formats
1444 	if ( ( pix_fmt == AV_PIX_FMT_YUVA420P
1445 #if defined(FFUDIV)
1446 			|| pix_fmt == AV_PIX_FMT_YUVA444P
1447 #endif
1448 			) &&
1449 		*format != mlt_image_rgba &&
1450 		frame->data[3] && frame->linesize[3] )
1451 	{
1452 		int i;
1453 		uint8_t *src, *dst;
1454 
1455 		dst = *alpha = mlt_pool_alloc( width * height );
1456 		src = frame->data[3];
1457 
1458 		for ( i = 0; i < height; dst += width, src += frame->linesize[3], i++ )
1459 			memcpy( dst, src, FFMIN( width, frame->linesize[3] ) );
1460 	}
1461 
1462 	int src_pix_fmt = pix_fmt;
1463 	pick_av_pixel_format( &src_pix_fmt );
1464 	if ( *format == mlt_image_yuv420p )
1465 	{
1466 		// This is a special case. Movit wants the full range, if available.
1467 		// Thankfully, there is not much other use of yuv420p except consumer
1468 		// avformat with no filters and explicitly requested.
1469 #if defined(FFUDIV)
1470 		int flags = mlt_get_sws_flags(width, height, src_pix_fmt, width, height, AV_PIX_FMT_YUV420P);
1471 		struct SwsContext *context = sws_getContext(width, height, src_pix_fmt,
1472 			width, height, AV_PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
1473 #else
1474 		int dst_pix_fmt = self->full_luma ? AV_PIX_FMT_YUVJ420P : AV_PIX_FMT_YUV420P;
1475 		int flags = mlt_get_sws_flags(width, height, pix_fmt, width, height, dst_pix_fmt);
1476 		struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1477 					width, height, dst_pix_fmt,
1478 					flags, NULL, NULL, NULL);
1479 #endif
1480 
1481 		uint8_t *out_data[4];
1482 		int out_stride[4];
1483 		out_data[0] = buffer;
1484 		out_data[1] = buffer + width * height;
1485 		out_data[2] = buffer + ( 5 * width * height ) / 4;
1486 		out_stride[0] = width;
1487 		out_stride[1] = width >> 1;
1488 		out_stride[2] = width >> 1;
1489 		if ( !mlt_set_luma_transfer( context, self->yuv_colorspace, profile->colorspace, self->full_luma, self->full_luma ) )
1490 			result = profile->colorspace;
1491 		sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1492 			out_data, out_stride);
1493 		sws_freeContext( context );
1494 	}
1495 	else if ( *format == mlt_image_rgb )
1496 	{
1497 		int flags = mlt_get_sws_flags(width, height, src_pix_fmt, width, height, AV_PIX_FMT_RGB24);
1498 		struct SwsContext *context = sws_getContext( width, height, src_pix_fmt,
1499 			width, height, AV_PIX_FMT_RGB24, flags, NULL, NULL, NULL);
1500 		uint8_t *out_data[4];
1501 		int out_stride[4];
1502 		av_image_fill_arrays(out_data, out_stride, buffer, AV_PIX_FMT_RGB24, width, height, IMAGE_ALIGN);
1503 		// libswscale wants the RGB colorspace to be SWS_CS_DEFAULT, which is = SWS_CS_ITU601.
1504 		mlt_set_luma_transfer( context, self->yuv_colorspace, 601, self->full_luma, 0 );
1505 		sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1506 			out_data, out_stride);
1507 		sws_freeContext( context );
1508 	}
1509 	else if ( *format == mlt_image_rgba )
1510 	{
1511 		int flags = mlt_get_sws_flags(width, height, src_pix_fmt, width, height, AV_PIX_FMT_RGBA);
1512 		struct SwsContext *context = sws_getContext( width, height, src_pix_fmt,
1513 			width, height, AV_PIX_FMT_RGBA, flags, NULL, NULL, NULL);
1514 		uint8_t *out_data[4];
1515 		int out_stride[4];
1516 		av_image_fill_arrays(out_data, out_stride, buffer, AV_PIX_FMT_RGBA, width, height, IMAGE_ALIGN);
1517 		// libswscale wants the RGB colorspace to be SWS_CS_DEFAULT, which is = SWS_CS_ITU601.
1518 		mlt_set_luma_transfer( context, self->yuv_colorspace, 601, self->full_luma, 0 );
1519 		sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1520 			out_data, out_stride);
1521 		sws_freeContext( context );
1522 	}
1523 	else
1524 #if defined(FFUDIV) && (LIBSWSCALE_VERSION_INT >= ((3<<16)+(1<<8)+101))
1525 	{
1526 		int i, c;
1527 		struct sliced_pix_fmt_conv_t ctx =
1528 		{
1529 			.width = width,
1530 			.height = height,
1531 			.frame = frame,
1532 			.dst_format = AV_PIX_FMT_YUYV422,
1533 			.src_colorspace = self->yuv_colorspace,
1534 			.dst_colorspace = profile->colorspace,
1535 			.src_full_range = self->full_luma,
1536 			.dst_full_range = 0,
1537 		};
1538 		ctx.src_format = (self->full_luma && src_pix_fmt == AV_PIX_FMT_YUV422P) ? AV_PIX_FMT_YUVJ422P : src_pix_fmt;
1539 		ctx.src_desc = av_pix_fmt_desc_get( ctx.src_format );
1540 		ctx.dst_desc = av_pix_fmt_desc_get( ctx.dst_format );
1541 		ctx.flags = mlt_get_sws_flags(width, height, ctx.src_format, width, height, ctx.dst_format);
1542 
1543 		av_image_fill_arrays(ctx.out_data, ctx.out_stride, buffer, ctx.dst_format, width, height, IMAGE_ALIGN);
1544 
1545 		int sliced = !getenv("MLT_AVFORMAT_SLICED_PIXFMT_DISABLE");
1546 		if ( sliced ) {
1547 			ctx.slice_w = ( width < 1000 )
1548 				? ( 256 >> frame->interlaced_frame )
1549 				: ( 512 >> frame->interlaced_frame );
1550 		} else {
1551 			ctx.slice_w = width;
1552 		}
1553 
1554 		c = ( width + ctx.slice_w - 1 ) / ctx.slice_w;
1555 		int last_slice_w = width - ctx.slice_w * (c - 1);
1556 
1557 		if ( sliced && (last_slice_w % 8) == 0 && !(ctx.src_format == AV_PIX_FMT_YUV422P && last_slice_w % 16) ) {
1558 			c *= frame->interlaced_frame ? 2 : 1;
1559 			mlt_slices_run_normal( c, sliced_h_pix_fmt_conv_proc, &ctx );
1560 		} else {
1561 			c = frame->interlaced_frame ? 2 : 1;
1562 			ctx.slice_w = width;
1563 			for ( i = 0 ; i < c; i++ )
1564 				sliced_h_pix_fmt_conv_proc( i, i, c, &ctx );
1565 		}
1566 
1567 		result = profile->colorspace;
1568 	}
1569 #else
1570 	{
1571 #if defined(FFUDIV)
1572 		int flags = mlt_get_sws_flags(width, height, src_pix_fmt, width, height, AV_PIX_FMT_YUYV422);
1573 		struct SwsContext *context = sws_getContext( width, height, src_pix_fmt,
1574 			width, height, AV_PIX_FMT_YUYV422, flags, NULL, NULL, NULL);
1575 #else
1576 		int flags = mlt_get_sws_flags(width, height, pix_fmt, width, height, AV_PIX_FMT_YUYV422);
1577 		struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1578 			width, height, AV_PIX_FMT_YUYV422, flags, NULL, NULL, NULL);
1579 #endif
1580 		AVPicture output;
1581 		avpicture_fill( &output, buffer, AV_PIX_FMT_YUYV422, width, height );
1582 		if ( !mlt_set_luma_transfer( context, self->yuv_colorspace, profile->colorspace, self->full_luma, 0 ) )
1583 			result = profile->colorspace;
1584 		sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1585 			output.data, output.linesize);
1586 		sws_freeContext( context );
1587 	}
1588 #endif
1589 	mlt_log_timings_end( NULL, __FUNCTION__ );
1590 
1591 	return result;
1592 }
1593 
set_image_size(producer_avformat self,int * width,int * height)1594 static void set_image_size( producer_avformat self, int *width, int *height )
1595 {
1596 	double dar = mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE(self->parent) ) );
1597 	double theta  = self->autorotate? get_rotation( self->video_format->streams[self->video_index] ) : 0.0;
1598 	if ( fabs(theta - 90.0) < 1.0 || fabs(theta - 270.0) < 1.0 )
1599 	{
1600 		*height = self->video_codec->width;
1601 		// Workaround 1088 encodings missing cropping info.
1602 		if ( self->video_codec->height == 1088 && dar == 16.0/9.0 )
1603 			*width = 1080;
1604 		else
1605 			*width = self->video_codec->height;
1606 	} else {
1607 		*width = self->video_codec->width;
1608 		// Workaround 1088 encodings missing cropping info.
1609 		if ( self->video_codec->height == 1088 && dar == 16.0/9.0 )
1610 			*height = 1080;
1611 		else
1612 			*height = self->video_codec->height;
1613 	}
1614 }
1615 
1616 /** Allocate the image buffer and set it on the frame.
1617 */
1618 
allocate_buffer(mlt_frame frame,AVCodecParameters * codec_params,uint8_t ** buffer,mlt_image_format format,int width,int height)1619 static int allocate_buffer( mlt_frame frame, AVCodecParameters *codec_params, uint8_t **buffer, mlt_image_format format, int width, int height )
1620 {
1621 	int size = 0;
1622 
1623 	if ( codec_params->width == 0 || codec_params->height == 0 )
1624 		return size;
1625 
1626 	size = mlt_image_format_size( format, width, height, NULL );
1627 	*buffer = mlt_pool_alloc( size );
1628 	if ( *buffer )
1629 		mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
1630 	else
1631 		size = 0;
1632 
1633 	return size;
1634 }
1635 
ignore_send_packet_result(int result)1636 static int ignore_send_packet_result(int result)
1637 {
1638 	return result >= 0 || result == AVERROR(EAGAIN) || result == AVERROR_EOF || result == AVERROR_INVALIDDATA;
1639 }
1640 
1641 /** Get an image from a frame.
1642 */
1643 
producer_get_image(mlt_frame frame,uint8_t ** buffer,mlt_image_format * format,int * width,int * height,int writable)1644 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
1645 {
1646 	// Get the producer
1647 	(void) writable; // unused
1648 	producer_avformat self = mlt_frame_pop_service( frame );
1649 	mlt_producer producer = self->parent;
1650 
1651 	// Get the properties from the frame
1652 	mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1653 
1654 	// Obtain the frame number of this frame
1655 	mlt_position position = mlt_frame_original_position( frame );
1656 
1657 	// Get the producer properties
1658 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1659 
1660 	uint8_t *alpha = NULL;
1661 	int got_picture = 0;
1662 	int image_size = 0;
1663 
1664 	pthread_mutex_lock( &self->video_mutex );
1665 	mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
1666 	mlt_log_timings_begin();
1667 
1668 	// Fetch the video format context
1669 	AVFormatContext *context = self->video_format;
1670 	AVCodecParameters *codec_params = NULL;
1671 	if ( !context )
1672 		goto exit_get_image;
1673 
1674 	// Get the video stream
1675 	AVStream *stream = context->streams[ self->video_index ];
1676 
1677 	// Get codec context
1678 	AVCodecContext *codec_context = stream->codec;
1679 	codec_params = stream->codecpar;
1680 
1681 	// Always use the image cache for album art.
1682 	int is_album_art = stream->disposition & AV_DISPOSITION_ATTACHED_PIC;
1683 	if (is_album_art)
1684 		position = 0;
1685 
1686 	// Get the image cache
1687 	if ( ! self->image_cache )
1688 	{
1689 		// if cache size supplied by environment variable
1690 		int cache_supplied = getenv( "MLT_AVFORMAT_CACHE" ) != NULL;
1691 		int cache_size = cache_supplied? atoi( getenv( "MLT_AVFORMAT_CACHE" ) ) : 0;
1692 
1693 		// cache size supplied via property
1694 		if ( mlt_properties_get( properties, "cache" ) )
1695 		{
1696 			cache_supplied = 1;
1697 			cache_size = mlt_properties_get_int( properties, "cache" );
1698 		}
1699 		if ( mlt_properties_get_int( properties, "noimagecache" ) )
1700 		{
1701 			cache_supplied = 1;
1702 			cache_size = 0;
1703 		}
1704 		// create cache if not disabled
1705 		if ( !cache_supplied || cache_size > 0 )
1706 			self->image_cache = mlt_cache_init();
1707 		// set cache size if supplied
1708 		if ( self->image_cache && cache_supplied )
1709 			mlt_cache_set_size( self->image_cache, cache_size );
1710 	}
1711 	if ( self->image_cache )
1712 	{
1713 		mlt_frame original = mlt_cache_get_frame( self->image_cache, position );
1714 		if ( original )
1715 		{
1716 			mlt_properties orig_props = MLT_FRAME_PROPERTIES( original );
1717 			int size = 0;
1718 
1719 			*buffer = mlt_properties_get_data( orig_props, "alpha", &size );
1720 			if (*buffer)
1721 				mlt_frame_set_alpha( frame, *buffer, size, NULL );
1722 			*buffer = mlt_properties_get_data( orig_props, "image", &size );
1723 			mlt_frame_set_image( frame, *buffer, size, NULL );
1724 			mlt_properties_set_data( frame_properties, "avformat.image_cache", original, 0, (mlt_destructor) mlt_frame_close, NULL );
1725 			*format = mlt_properties_get_int( orig_props, "format" );
1726 			set_image_size( self, width, height );
1727 			mlt_properties_pass_property(frame_properties, orig_props, "colorspace");
1728 			got_picture = 1;
1729 			goto exit_get_image;
1730 		}
1731 	}
1732 	// Cache miss
1733 
1734 	// We may want to use the source fps if available
1735 	double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1736 		mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1737 
1738 	// This is the physical frame position in the source
1739 	int64_t req_position = ( int64_t )( position / mlt_producer_get_fps( producer ) * source_fps + 0.5 );
1740 
1741 	// Determines if we have to decode all frames in a sequence - when there temporal compression is used.
1742 	const AVCodecDescriptor *descriptor = avcodec_descriptor_get( codec_params->codec_id );
1743 	int must_decode = descriptor && !( descriptor->props & AV_CODEC_PROP_INTRA_ONLY );
1744 
1745 	double delay = mlt_properties_get_double( properties, "video_delay" );
1746 
1747 	// Seek if necessary
1748 	double speed = mlt_producer_get_speed(producer);
1749 	int preseek = must_decode && codec_context->has_b_frames && speed >= 0.0 && speed <= 1.0;
1750 	int paused = seek_video( self, position, req_position, preseek );
1751 
1752 	// Seek might have reopened the file
1753 	context = self->video_format;
1754 	stream = context->streams[ self->video_index ];
1755 	codec_context = stream->codec;
1756 	codec_params = stream->codecpar;
1757 	if ( *format == mlt_image_none || *format == mlt_image_movit ||
1758 			codec_params->format == AV_PIX_FMT_ARGB ||
1759 			codec_params->format == AV_PIX_FMT_RGBA ||
1760 			codec_params->format == AV_PIX_FMT_ABGR ||
1761 			codec_params->format == AV_PIX_FMT_BGRA )
1762 		*format = pick_image_format( codec_params->format );
1763 #if defined(FFUDIV)
1764 	else if ( codec_params->format == AV_PIX_FMT_BAYER_RGGB16LE ) {
1765 		if ( *format == mlt_image_yuv422 )
1766 			*format = mlt_image_yuv420p;
1767 		else if ( *format == mlt_image_rgba )
1768 			*format = mlt_image_rgb;
1769 	}
1770 #endif
1771 	else if ( codec_params->format == AV_PIX_FMT_YUVA444P10LE
1772 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56,0,0)
1773 			|| codec_params->format == AV_PIX_FMT_GBRAP10LE
1774 			|| codec_params->format == AV_PIX_FMT_GBRAP12LE
1775 #endif
1776 			)
1777 		*format = mlt_image_rgba;
1778 
1779 	// Duplicate the last image if necessary
1780 	if ( self->video_frame && self->video_frame->linesize[0]
1781 		 && (self->pkt.stream_index == self->video_index )
1782 		 && ( paused || self->current_position >= req_position ) )
1783 	{
1784 		// Duplicate it
1785 		set_image_size( self, width, height );
1786 		if ( ( image_size = allocate_buffer( frame, codec_params, buffer, *format, *width, *height ) ) )
1787 		{
1788 			int yuv_colorspace;
1789 #if USE_HWACCEL
1790 			yuv_colorspace = convert_image( self, self->video_frame, *buffer, self->video_frame->format,
1791 				format, *width, *height, &alpha );
1792 #else
1793 			yuv_colorspace = convert_image( self, self->video_frame, *buffer, codec_params->format,
1794 				format, *width, *height, &alpha );
1795 #endif
1796 			mlt_properties_set_int( frame_properties, "colorspace", yuv_colorspace );
1797 			got_picture = 1;
1798 		}
1799 	}
1800 	else
1801 	{
1802 		int64_t int_position = 0;
1803 		int decode_errors = 0;
1804 
1805 		// Construct an AVFrame for YUV422 conversion
1806 		if ( !self->video_frame )
1807 			self->video_frame = av_frame_alloc();
1808 		else
1809 			av_frame_unref( self->video_frame );
1810 
1811 		while (!got_picture && ignore_send_packet_result(self->video_send_result))
1812 		{
1813 			if ( self->video_send_result != AVERROR( EAGAIN ) )
1814 			{
1815 				// Read a packet
1816 				if ( self->pkt.stream_index == self->video_index )
1817 					av_packet_unref( &self->pkt );
1818 				av_init_packet( &self->pkt );
1819 				pthread_mutex_lock( &self->packets_mutex );
1820 				if ( mlt_deque_count( self->vpackets ) )
1821 				{
1822 					AVPacket *tmp = (AVPacket*) mlt_deque_pop_front( self->vpackets );
1823 					av_packet_ref( &self->pkt, tmp );
1824 					av_packet_free( &tmp );
1825 				}
1826 				else
1827 				{
1828 					int ret = av_read_frame( context, &self->pkt );
1829 					if ( ret >= 0 && !self->video_seekable && self->pkt.stream_index == self->audio_index )
1830 					{
1831 						mlt_deque_push_back( self->apackets, av_packet_clone( &self->pkt ) );
1832 					}
1833 					else if ( ret < 0 )
1834 					{
1835 						if ( ret == AVERROR_EOF )
1836 						{
1837 							self->pkt.stream_index = self->video_index;
1838 						} else
1839 						{
1840 							mlt_log_verbose( MLT_PRODUCER_SERVICE( producer ), "av_read_frame returned error %d inside get_image\n", ret );
1841 						}
1842 						if ( !self->video_seekable && mlt_properties_get_int( properties, "reconnect" ) )
1843 						{
1844 							// Try to reconnect to live sources by closing context and codecs,
1845 							// and letting next call to get_frame() reopen.
1846 							mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
1847 							prepare_reopen( self );
1848 							mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
1849 							pthread_mutex_unlock( &self->packets_mutex );
1850 							goto exit_get_image;
1851 						}
1852 						if ( !self->video_seekable && mlt_properties_get_int( properties, "exit_on_disconnect" ) )
1853 						{
1854 							mlt_log_fatal( MLT_PRODUCER_SERVICE( producer ), "Exiting with error due to disconnected source.\n" );
1855 							exit( EXIT_FAILURE );
1856 						}
1857 						// Send null packets to drain decoder.
1858 						self->pkt.size = 0;
1859 						self->pkt.data = NULL;
1860 					}
1861 				}
1862 				pthread_mutex_unlock( &self->packets_mutex );
1863 			}
1864 
1865 			// We only deal with video from the selected video_index
1866 			if ( self->pkt.stream_index == self->video_index )
1867 			{
1868 				int64_t pts = best_pts( self, self->pkt.pts, self->pkt.dts );
1869 				if ( pts != AV_NOPTS_VALUE )
1870 				{
1871 					if ( !self->video_seekable && self->first_pts == AV_NOPTS_VALUE )
1872 						self->first_pts = pts;
1873 					if ( self->first_pts != AV_NOPTS_VALUE )
1874 						pts -= self->first_pts;
1875 					else if ( context->start_time != AV_NOPTS_VALUE )
1876 						pts -= context->start_time;
1877 					int_position = ( int64_t )( ( av_q2d( self->video_time_base ) * pts + delay ) * source_fps + 0.5 );
1878 					if ( int_position == self->last_position )
1879 						int_position = self->last_position + 1;
1880 				}
1881 				mlt_log_debug( MLT_PRODUCER_SERVICE(producer),
1882 					"V pkt.pts %"PRId64" pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n",
1883 					self->pkt.pts, self->pkt.dts, req_position, self->current_position, int_position );
1884 
1885 				// Make a dumb assumption on streams that contain wild timestamps
1886 				if ( llabs( req_position - int_position ) > 999 )
1887 				{
1888 					mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP: "
1889 						"pkt.pts=[%"PRId64"], pkt.dts=[%"PRId64"], req_position=[%"PRId64"], "
1890 						"current_position=[%"PRId64"], int_position=[%"PRId64"], pts=[%"PRId64"] \n",
1891 						self->pkt.pts, self->pkt.dts, req_position,
1892 						self->current_position, int_position, pts );
1893 					int_position = req_position;
1894 				}
1895 				self->last_position = int_position;
1896 
1897 				// Decode the image
1898 				if ( must_decode  || int_position >= req_position || !self->pkt.data )
1899 				{
1900 					codec_context->reordered_opaque = int_position;
1901 					if ( int_position >= req_position )
1902 						codec_context->skip_loop_filter = AVDISCARD_NONE;
1903 					self->video_send_result = avcodec_send_packet( codec_context, &self->pkt );
1904 					mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "decoded video packet with size %d => %d\n", self->pkt.size, self->video_send_result );
1905 					// Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1906 					if (!ignore_send_packet_result(self->video_send_result))
1907 					{
1908 						mlt_log_warning( MLT_PRODUCER_SERVICE( producer ), "video avcodec_send_packet failed with %d\n", self->video_send_result );
1909 					}
1910 					else
1911 					{
1912 						int error = avcodec_receive_frame( codec_context, self->video_frame );
1913 						if ( error < 0 )
1914 						{
1915 							if ( error != AVERROR( EAGAIN ) && ++decode_errors > 10 )
1916 							{
1917 								mlt_log_warning( MLT_PRODUCER_SERVICE( producer ), "video decoding error %d\n", error );
1918 								self->last_good_position = POSITION_INVALID;
1919 							}
1920 						}
1921 						else
1922 						{
1923 #if USE_HWACCEL
1924 							if (self->hwaccel.device_ctx && self->video_frame->format == self->hwaccel.pix_fmt)
1925 							{
1926 								AVFrame *sw_video_frame = av_frame_alloc();
1927 								int transfer_data_result = av_hwframe_transfer_data(sw_video_frame, self->video_frame, 0);
1928 								if(transfer_data_result < 0)
1929 								{
1930 									mlt_log_error( MLT_PRODUCER_SERVICE(producer), "av_hwframe_transfer_data() failed %d\n", transfer_data_result);
1931 									av_frame_free(&sw_video_frame);
1932 									goto exit_get_image;
1933 								}
1934 								av_frame_copy_props(sw_video_frame, self->video_frame);
1935 								sw_video_frame->width = self->video_frame->width;
1936 								sw_video_frame->height = self->video_frame->height;
1937 
1938 								av_frame_unref(self->video_frame);
1939 								av_frame_move_ref(self->video_frame, sw_video_frame);
1940 								av_frame_free(&sw_video_frame);
1941 							}
1942 #endif
1943 							got_picture = 1;
1944 							decode_errors = 0;
1945 						}
1946 					}
1947 				}
1948 
1949 				if ( got_picture )
1950 				{
1951 					// Get position of reordered frame
1952 					int_position = self->video_frame->reordered_opaque;
1953 					pts = best_pts( self, self->video_frame->pts, self->video_frame->pkt_dts );
1954 					if ( pts != AV_NOPTS_VALUE )
1955 					{
1956 						// Some streams are not marking their key frames even though
1957 						// there are I frames, and find_first_pts() fails as a result.
1958 						// Try to set first_pts here after getting pict_type.
1959 						if ( self->first_pts == AV_NOPTS_VALUE &&
1960 							(self->video_frame->key_frame || self->video_frame->pict_type == AV_PICTURE_TYPE_I) )
1961 							 self->first_pts = pts;
1962 						if ( self->first_pts != AV_NOPTS_VALUE )
1963 							pts -= self->first_pts;
1964 						else if ( context->start_time != AV_NOPTS_VALUE )
1965 							pts -= context->start_time;
1966 						int_position = ( int64_t )( ( av_q2d( self->video_time_base ) * pts + delay ) * source_fps + 0.5 );
1967 					}
1968 
1969 					if ( int_position < req_position )
1970 						got_picture = 0;
1971 					else if ( int_position >= req_position )
1972 						codec_context->skip_loop_filter = AVDISCARD_NONE;
1973 				}
1974 				else if ( !self->pkt.data ) // draining decoder with null packets
1975 				{
1976 					self->video_send_result = -1;
1977 				}
1978 				mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), " got_pic %d key %d send_result %d pkt_pos %"PRId64"\n",
1979 							   got_picture, self->pkt.flags & AV_PKT_FLAG_KEY, self->video_send_result, int_position );
1980 			}
1981 
1982 			// Now handle the picture if we have one
1983 			if ( got_picture )
1984 			{
1985 #ifdef AVFILTER
1986 				if (self->autorotate && self->vfilter_graph) {
1987 					int ret = av_buffersrc_add_frame(self->vfilter_in, self->video_frame);
1988 					if (ret < 0) {
1989 						got_picture = 0;
1990 						break;
1991 					}
1992 					while (ret >= 0) {
1993 						ret = av_buffersink_get_frame_flags(self->vfilter_out, self->video_frame, 0);
1994 						if (ret < 0) {
1995 							ret = 0;
1996 							break;
1997 						}
1998 					}
1999 				}
2000 #endif
2001 				set_image_size( self, width, height );
2002 				if ( ( image_size = allocate_buffer( frame, codec_params, buffer, *format, *width, *height ) ) )
2003 				{
2004 					int yuv_colorspace;
2005 #if USE_HWACCEL
2006 					// not sure why this is really needed, but doesn't seem to work otherwise
2007 					yuv_colorspace = convert_image( self, self->video_frame, *buffer, self->video_frame->format,
2008 						format, *width, *height, &alpha );
2009 #else
2010 					yuv_colorspace = convert_image( self, self->video_frame, *buffer, codec_params->format,
2011 						format, *width, *height, &alpha );
2012 #endif
2013 					mlt_properties_set_int( frame_properties, "colorspace", yuv_colorspace );
2014 					self->top_field_first |= self->video_frame->top_field_first;
2015 					self->top_field_first |= codec_params->field_order == AV_FIELD_TT;
2016 					self->top_field_first |= codec_params->field_order == AV_FIELD_TB;
2017 					self->current_position = int_position;
2018 				}
2019 				else
2020 				{
2021 					got_picture = 0;
2022 				}
2023 			}
2024 
2025 			// Free packet data if not video and not live audio packet
2026 			if ( self->pkt.stream_index != self->video_index &&
2027 				 !( !self->video_seekable && self->pkt.stream_index == self->audio_index ) )
2028 				av_packet_unref( &self->pkt );
2029 		}
2030 	}
2031 
2032 	// set alpha
2033 	if ( alpha )
2034 		mlt_frame_set_alpha( frame, alpha, (*width) * (*height), mlt_pool_release );
2035 
2036 	if ( image_size > 0 )
2037 	{
2038 		mlt_properties_set_int( frame_properties, "format", *format );
2039 		// Cache the image for rapid repeated access.
2040 		if ( self->image_cache ) {
2041 			if (is_album_art) {
2042 				mlt_position original_pos = mlt_frame_original_position( frame );
2043 				mlt_properties_set_position(frame_properties, "original_position", 0);
2044 				mlt_cache_put_frame( self->image_cache, frame );
2045 				mlt_properties_set_position(frame_properties, "original_position", original_pos);
2046 			} else {
2047 				mlt_cache_put_frame( self->image_cache, frame );
2048 			}
2049 		}
2050 		// Clone frame for error concealment.
2051 		if ( self->current_position >= self->last_good_position ) {
2052 			self->last_good_position = self->current_position;
2053 			if ( self->last_good_frame )
2054 				mlt_frame_close( self->last_good_frame );
2055 			self->last_good_frame = mlt_frame_clone( frame, 1 );
2056 		}
2057 	}
2058 	else if ( self->last_good_frame )
2059 	{
2060 		// Use last known good frame if there was a decoding failure.
2061 		mlt_frame original = mlt_frame_clone( self->last_good_frame, 1 );
2062 		mlt_properties orig_props = MLT_FRAME_PROPERTIES( original );
2063 		int size = 0;
2064 
2065 		*buffer = mlt_properties_get_data( orig_props, "alpha", &size );
2066 		if (*buffer)
2067 			mlt_frame_set_alpha( frame, *buffer, size, NULL );
2068 		*buffer = mlt_properties_get_data( orig_props, "image", &size );
2069 		mlt_frame_set_image( frame, *buffer, size, NULL );
2070 		mlt_properties_set_data( frame_properties, "avformat.conceal_error", original, 0, (mlt_destructor) mlt_frame_close, NULL );
2071 		*format = mlt_properties_get_int( orig_props, "format" );
2072 		set_image_size( self, width, height );
2073 		got_picture = 1;
2074 	}
2075 
2076 	// Regardless of speed, we expect to get the next frame (cos we ain't too bright)
2077 	self->video_expected = position + 1;
2078 
2079 exit_get_image:
2080 	pthread_mutex_unlock( &self->video_mutex );
2081 
2082 	// Set the progressive flag
2083 	if ( mlt_properties_get( properties, "force_progressive" ) ) {
2084 		mlt_properties_set_int( frame_properties, "progressive", !!mlt_properties_get_int( properties, "force_progressive" ) );
2085 	} else if ( self->video_frame && codec_params ) {
2086 		mlt_properties_set_int( frame_properties, "progressive",
2087 			!self->video_frame->interlaced_frame &&
2088 				(codec_params->field_order == AV_FIELD_PROGRESSIVE ||
2089 				 codec_params->field_order == AV_FIELD_UNKNOWN) );
2090 	}
2091 
2092 	// Set the field order property for this frame
2093 	if ( mlt_properties_get( properties, "force_tff" ) )
2094 		mlt_properties_set_int( frame_properties, "top_field_first", !!mlt_properties_get_int( properties, "force_tff" ) );
2095 	else
2096 		mlt_properties_set_int( frame_properties, "top_field_first", self->top_field_first );
2097 
2098 	// Set immutable properties of the selected track's (or overridden) source attributes.
2099 	mlt_properties_set_int( properties, "meta.media.top_field_first", self->top_field_first );
2100 	mlt_properties_set_int( properties, "meta.media.progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
2101 	mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
2102 
2103 	mlt_log_timings_end( NULL, __FUNCTION__ );
2104 
2105 	return !got_picture;
2106 }
2107 
2108 /** Process properties as AVOptions and apply to AV context obj
2109 */
2110 
apply_properties(void * obj,mlt_properties properties,int flags)2111 static void apply_properties( void *obj, mlt_properties properties, int flags )
2112 {
2113 	int i;
2114 	int count = mlt_properties_count( properties );
2115 	for ( i = 0; i < count; i++ )
2116 	{
2117 		const char *opt_name = mlt_properties_get_name( properties, i );
2118 		int search_flags = AV_OPT_SEARCH_CHILDREN;
2119 		const AVOption *opt = av_opt_find( obj, opt_name, NULL, flags, search_flags );
2120 		if ( opt_name && mlt_properties_get( properties, opt_name ) && strcmp(opt_name, "seekable") )
2121 		{
2122 			if ( opt )
2123 				av_opt_set( obj, opt_name, mlt_properties_get( properties, opt_name), search_flags );
2124 		}
2125 	}
2126 }
2127 
2128 /** Initialize the video codec context.
2129  */
2130 
video_codec_init(producer_avformat self,int index,mlt_properties properties)2131 static int video_codec_init( producer_avformat self, int index, mlt_properties properties )
2132 {
2133 	// Initialise the codec if necessary
2134 	if ( !self->video_codec )
2135 	{
2136 		// Get the video stream
2137 		AVStream *stream = self->video_format->streams[ index ];
2138 
2139 		// Get codec context
2140 		AVCodecContext *codec_context = stream->codec;
2141 		AVCodecParameters *codec_params = stream->codecpar;
2142 
2143 		// Find the codec
2144 		AVCodec *codec = avcodec_find_decoder( codec_params->codec_id );
2145 		if ( mlt_properties_get( properties, "vcodec" ) ) {
2146 			if ( !( codec = avcodec_find_decoder_by_name( mlt_properties_get( properties, "vcodec" ) ) ) )
2147 				codec = avcodec_find_decoder( codec_params->codec_id );
2148 		} else if ( codec_params->codec_id == AV_CODEC_ID_VP9 ) {
2149 			if ( !( codec = avcodec_find_decoder_by_name( "libvpx-vp9" ) ) )
2150 				codec = avcodec_find_decoder( codec_params->codec_id );
2151 		} else if ( codec_params->codec_id == AV_CODEC_ID_VP8 ) {
2152 			if ( !( codec = avcodec_find_decoder_by_name( "libvpx" ) ) )
2153 				codec = avcodec_find_decoder( codec_params->codec_id );
2154 		}
2155 
2156 		// Initialise multi-threading
2157 		int thread_count = mlt_properties_get_int( properties, "threads" );
2158 		if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
2159 			thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
2160 		if ( thread_count >= 0 )
2161 			codec_context->thread_count = thread_count;
2162 
2163 #if USE_HWACCEL
2164 		if ( self->hwaccel.device_type == AV_HWDEVICE_TYPE_NONE || self->hwaccel.pix_fmt == AV_PIX_FMT_NONE )
2165 		{
2166 			mlt_log_verbose( MLT_PRODUCER_SERVICE( self->parent ), "missing hwaccel parameters. skipping hardware initialization\n" );
2167 			goto skip_hwaccel;
2168 		}
2169 
2170 		int found_hw_pix_fmt = 0, i;
2171 		for ( i = 0;; i++ )
2172 		{
2173 			const AVCodecHWConfig *config = avcodec_get_hw_config( codec, i );
2174 			if ( !config )
2175 				break;
2176 
2177 			if ( config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
2178 				config->device_type == self->hwaccel.device_type && config->pix_fmt == self->hwaccel.pix_fmt )
2179 			{
2180 				found_hw_pix_fmt = 1;
2181 				break;
2182 			}
2183 		}
2184 
2185 		if ( found_hw_pix_fmt )
2186 		{
2187 			av_buffer_unref( &self->hwaccel.device_ctx );
2188 			int ret = av_hwdevice_ctx_create( &self->hwaccel.device_ctx, self->hwaccel.device_type, self->hwaccel.device, NULL, 0 );
2189 			if ( ret >= 0 )
2190 			{
2191 				codec_context->hw_device_ctx = av_buffer_ref( self->hwaccel.device_ctx );
2192 				mlt_log_info( MLT_PRODUCER_SERVICE( self->parent ), "av_hwdevice_ctx_create() success %d\n", codec_context->pix_fmt );
2193 			}
2194 			else
2195 			{
2196 				mlt_log_warning( MLT_PRODUCER_SERVICE( self->parent ), "av_hwdevice_ctx_create() failed %d\n", ret );
2197 			}
2198 		}
2199 		else
2200 		{
2201 			mlt_log_warning( MLT_PRODUCER_SERVICE( self->parent ), "failed to find hw_pix_fmt\n" );
2202 		}
2203 
2204 		skip_hwaccel:
2205 #endif
2206 		// If we don't have a codec and we can't initialise it, we can't do much more...
2207 		pthread_mutex_lock( &self->open_mutex );
2208 		if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
2209 		{
2210 			// Switch to the native vp8/vp9 decoder if not yuva420p
2211 			if ( codec_params->format != AV_PIX_FMT_YUVA420P
2212 				 && !mlt_properties_get( properties, "vcodec" )
2213 				 && ( !strcmp(codec->name, "libvpx") || !strcmp(codec->name, "libvpx-vp9") ) )
2214 			{
2215 				codec = avcodec_find_decoder( codec_params->codec_id );
2216 				if ( codec && avcodec_open2( codec_context, codec, NULL ) < 0 ) {
2217 					self->video_index = -1;
2218 					pthread_mutex_unlock( &self->open_mutex );
2219 					return 0;
2220 				}
2221 			}
2222 			// Now store the codec with its destructor
2223 			self->video_codec = codec_context;
2224 		}
2225 		else
2226 		{
2227 			// Remember that we can't use this later
2228 			self->video_index = -1;
2229 			pthread_mutex_unlock( &self->open_mutex );
2230 			return 0;
2231 		}
2232 		pthread_mutex_unlock( &self->open_mutex );
2233 
2234 		// Process properties as AVOptions
2235 		apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2236 		if ( codec && codec->priv_class && codec_context->priv_data )
2237 			apply_properties( codec_context->priv_data, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2238 
2239 		// Reset some image properties
2240 		mlt_properties_set_int( properties, "width", codec_params->width );
2241 		mlt_properties_set_int( properties, "height", codec_params->height );
2242 		get_aspect_ratio( properties, stream, codec_params );
2243 
2244 		// Start with the muxer frame rate.
2245 		AVRational frame_rate = stream->avg_frame_rate;
2246 		double fps = av_q2d( frame_rate );
2247 
2248 #if defined(FFUDIV)
2249 		// Verify and sanitize the muxer frame rate.
2250 		if ( isnan( fps ) || isinf( fps ) || fps == 0 )
2251 		{
2252 			frame_rate = stream->r_frame_rate;
2253 			fps = av_q2d( frame_rate );
2254 		}
2255 		// With my samples when r_frame_rate != 1000 but avg_frame_rate is valid,
2256 		// avg_frame_rate gives some approximate value that does not well match the media.
2257 		// Also, on my sample where r_frame_rate = 1000, using avg_frame_rate directly
2258 		// results in some very choppy output, but some value slightly different works
2259 		// great.
2260 		if ( av_q2d( stream->r_frame_rate ) >= 1000 && av_q2d( stream->avg_frame_rate ) > 0 )
2261 		{
2262 			frame_rate = av_d2q( av_q2d( stream->avg_frame_rate ), 1024 );
2263 			fps = av_q2d( frame_rate );
2264 		}
2265 #endif
2266 		// XXX frame rates less than 1 fps are not considered sane
2267 		if ( isnan( fps ) || isinf( fps ) || fps < 1.0 )
2268 		{
2269 			// Get the frame rate from the codec.
2270 			frame_rate.num = self->video_codec->time_base.den;
2271 			frame_rate.den = self->video_codec->time_base.num * self->video_codec->ticks_per_frame;
2272 			fps = av_q2d( frame_rate );
2273 		}
2274 		if ( isnan( fps ) || isinf( fps ) || fps < 1.0 )
2275 		{
2276 			// Use the profile frame rate if all else fails.
2277 			mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self->parent ) );
2278 			frame_rate.num = profile->frame_rate_num;
2279 			frame_rate.den = profile->frame_rate_den;
2280 		}
2281 
2282 		self->video_time_base = stream->time_base;
2283 		if ( mlt_properties_get( properties, "force_fps" ) )
2284 		{
2285 			AVRational force_fps = av_d2q( mlt_properties_get_double( properties, "force_fps" ), 1024 );
2286 			self->video_time_base = av_mul_q( stream->time_base, av_div_q( frame_rate, force_fps ) );
2287 			frame_rate = force_fps;
2288 		}
2289 		mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
2290 		mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
2291 
2292 		// Cover art is a single image at 90000 fps, which is not seekable.
2293 		if (stream->disposition & AV_DISPOSITION_ATTACHED_PIC)
2294 			self->video_seekable = 0;
2295 
2296 		// Set the YUV colorspace from override or detect
2297 		self->yuv_colorspace = mlt_properties_get_int( properties, "force_colorspace" );
2298 		if ( ! self->yuv_colorspace )
2299 		{
2300 			switch ( self->video_codec->colorspace )
2301 			{
2302 			case AVCOL_SPC_SMPTE240M:
2303 				self->yuv_colorspace = 240;
2304 				break;
2305 			case AVCOL_SPC_BT470BG:
2306 			case AVCOL_SPC_SMPTE170M:
2307 				self->yuv_colorspace = 601;
2308 				break;
2309 			case AVCOL_SPC_BT709:
2310 				self->yuv_colorspace = 709;
2311 				break;
2312 			default:
2313 				// This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
2314 				self->yuv_colorspace = self->video_codec->width * self->video_codec->height > 750000 ? 709 : 601;
2315 				break;
2316 			}
2317 		}
2318 		// Let apps get chosen colorspace
2319 		mlt_properties_set_int( properties, "meta.media.colorspace", self->yuv_colorspace );
2320 
2321 		// Get the color transfer characteristic (gamma).
2322 		self->color_trc = mlt_properties_get_int( properties, "force_color_trc" );
2323 		if ( !self->color_trc )
2324 			self->color_trc = self->video_codec->color_trc;
2325 		mlt_properties_set_int( properties, "meta.media.color_trc", self->color_trc );
2326 
2327 		// Get the RGB color primaries.
2328 		switch ( self->video_codec->color_primaries )
2329 		{
2330 		case AVCOL_PRI_BT470BG:
2331 			self->color_primaries = 601625;
2332 			break;
2333 		case AVCOL_PRI_SMPTE170M:
2334 		case AVCOL_PRI_SMPTE240M:
2335 			self->color_primaries = 601525;
2336 			break;
2337 		case AVCOL_PRI_BT709:
2338 		case AVCOL_PRI_UNSPECIFIED:
2339 		default:
2340 			self->color_primaries = 709;
2341 			break;
2342 		}
2343 
2344 		mlt_properties_set_int( properties, "meta.media.has_b_frames", self->video_codec->has_b_frames );
2345 
2346 		self->full_luma = 0;
2347 		mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), "color_range %d\n", codec_context->color_range );
2348 		if ( codec_context->color_range == AVCOL_RANGE_JPEG )
2349 			self->full_luma = 1;
2350 		if ( mlt_properties_get( properties, "set.force_full_luma" ) )
2351 			self->full_luma = mlt_properties_get_int( properties, "set.force_full_luma" );
2352 	}
2353 	return self->video_index > -1;
2354 }
2355 
2356 /** Set up video handling.
2357 */
2358 
producer_set_up_video(producer_avformat self,mlt_frame frame)2359 static void producer_set_up_video( producer_avformat self, mlt_frame frame )
2360 {
2361 	// Get the producer
2362 	mlt_producer producer = self->parent;
2363 
2364 	// Get the properties
2365 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2366 
2367 	// Fetch the video format context
2368 	AVFormatContext *context = self->video_format;
2369 
2370 	// Get the video_index
2371 	int index = mlt_properties_get_int( properties, "video_index" );
2372 
2373 	int unlock_needed = 0;
2374 
2375 	// Reopen the file if necessary
2376 	if ( !context && index > -1 )
2377 	{
2378 		unlock_needed = 1;
2379 		pthread_mutex_lock( &self->video_mutex );
2380 		producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2381 			mlt_properties_get( properties, "resource" ), 0, 0 );
2382 		context = self->video_format;
2383 	}
2384 
2385 	// Exception handling for video_index
2386 	if ( context && index >= (int) context->nb_streams )
2387 	{
2388 		// Get the last video stream
2389 		for ( index = context->nb_streams - 1;
2390 			  index >= 0 && context->streams[ index ]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO;
2391 			  index-- );
2392 		mlt_properties_set_int( properties, "video_index", index );
2393 	}
2394 	if ( context && index > -1 && context->streams[ index ]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO )
2395 	{
2396 		// Invalidate the video stream
2397 		index = -1;
2398 		mlt_properties_set_int( properties, "video_index", index );
2399 	}
2400 
2401 	// Update the video properties if the index changed
2402 	if ( context && index > -1 && index != self->video_index )
2403 	{
2404 		// Reset the video properties if the index changed
2405 		self->video_index = index;
2406 		pthread_mutex_lock( &self->open_mutex );
2407 		if ( self->video_codec )
2408 			avcodec_close( self->video_codec );
2409 		self->video_codec = NULL;
2410 		pthread_mutex_unlock( &self->open_mutex );
2411 	}
2412 
2413 	// Get the frame properties
2414 	mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2415 
2416 	// Get the codec
2417 	if ( context && index > -1 && video_codec_init( self, index, properties ) )
2418 	{
2419 		// Set the frame properties
2420 		double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
2421 		double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
2422 			force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
2423 
2424 		// Set the width and height
2425 		double dar = mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) );
2426 		double theta  = self->autorotate? get_rotation( self->video_format->streams[index] ) : 0.0;
2427 		if ( fabs(theta - 90.0) < 1.0 || fabs(theta - 270.0) < 1.0 )
2428 		{
2429 			// Workaround 1088 encodings missing cropping info.
2430 			if ( self->video_codec->height == 1088 && dar == 16.0/9.0 ) {
2431 				mlt_properties_set_int( frame_properties, "width", 1080 );
2432 				mlt_properties_set_int( properties, "meta.media.width", 1080 );
2433 			} else {
2434 				mlt_properties_set_int( frame_properties, "width", self->video_codec->height );
2435 				mlt_properties_set_int( properties, "meta.media.width", self->video_codec->height );
2436 			}
2437 			mlt_properties_set_int( frame_properties, "height", self->video_codec->width );
2438 			mlt_properties_set_int( properties, "meta.media.height", self->video_codec->width );
2439 			aspect_ratio = ( force_aspect_ratio > 0.0 ) ? force_aspect_ratio : 1.0 / aspect_ratio;
2440 			mlt_properties_set_double( frame_properties, "aspect_ratio", 1.0/aspect_ratio );
2441 		} else {
2442 			mlt_properties_set_int( frame_properties, "width", self->video_codec->width );
2443 			mlt_properties_set_int( properties, "meta.media.width", self->video_codec->width );
2444 			// Workaround 1088 encodings missing cropping info.
2445 			if ( self->video_codec->height == 1088 && dar == 16.0/9.0 ) {
2446 				mlt_properties_set_int( frame_properties, "height", 1080 );
2447 				mlt_properties_set_int( properties, "meta.media.height", 1080 );
2448 			} else {
2449 				mlt_properties_set_int( frame_properties, "height", self->video_codec->height );
2450 				mlt_properties_set_int( properties, "meta.media.height", self->video_codec->height );
2451 			}
2452 			mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
2453 		}
2454 		mlt_properties_set_int( frame_properties, "colorspace", self->yuv_colorspace );
2455 		mlt_properties_set_int( frame_properties, "color_trc", self->color_trc );
2456 		mlt_properties_set_int( frame_properties, "color_primaries", self->color_primaries );
2457 		mlt_properties_set_int( frame_properties, "full_luma", self->full_luma );
2458 		mlt_properties_set( properties, "meta.media.color_range", self->full_luma? "full" : "mpeg" );
2459 
2460 		// Add our image operation
2461 		mlt_frame_push_service( frame, self );
2462 		mlt_frame_push_get_image( frame, producer_get_image );
2463 	}
2464 	else
2465 	{
2466 		// If something failed, use test card image
2467 		mlt_properties_set_int( frame_properties, "test_image", 1 );
2468 	}
2469 	if ( unlock_needed )
2470 		pthread_mutex_unlock( &self->video_mutex );
2471 }
2472 
seek_audio(producer_avformat self,mlt_position position,double timecode)2473 static int seek_audio( producer_avformat self, mlt_position position, double timecode )
2474 {
2475 	int paused = 0;
2476 
2477 	pthread_mutex_lock( &self->packets_mutex );
2478 
2479 	// Seek if necessary
2480 	if ( self->seekable && ( position != self->audio_expected || self->last_position < 0 ) )
2481 	{
2482 		if ( self->last_position == POSITION_INITIAL )
2483 		{
2484 			int video_index = self->video_index;
2485 			if ( video_index == -1 )
2486 				video_index = first_video_index( self );
2487 			if ( self->first_pts == AV_NOPTS_VALUE && video_index >= 0 )
2488 				find_first_pts( self, video_index );
2489 		}
2490 
2491 		if ( position + 1 == self->audio_expected  &&
2492 			mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( self->parent ), "mute_on_pause" ) )
2493 		{
2494 			// We're paused - silence required
2495 			paused = 1;
2496 		}
2497 		else if ( position < self->audio_expected || position - self->audio_expected >= 12 )
2498 		{
2499 			AVFormatContext *context = self->audio_format;
2500 			int64_t timestamp = llrint( timecode * AV_TIME_BASE );
2501 			if ( context->start_time != AV_NOPTS_VALUE )
2502 				timestamp += context->start_time;
2503 			if ( timestamp < 0 )
2504 				timestamp = 0;
2505 
2506 			// Set to the real timecode
2507 			if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
2508 				paused = 1;
2509 
2510 			// Clear the usage in the audio buffer
2511 			int i = MAX_AUDIO_STREAMS + 1;
2512 			while ( --i )
2513 				self->audio_used[i - 1] = 0;
2514 		}
2515 	}
2516 	pthread_mutex_unlock( &self->packets_mutex );
2517 	return paused;
2518 }
2519 
sample_bytes(AVCodecContext * context)2520 static int sample_bytes( AVCodecContext *context )
2521 {
2522 	return av_get_bytes_per_sample( context->sample_fmt );
2523 }
2524 
planar_to_interleaved(uint8_t * dest,AVFrame * src,int samples,int channels,int bytes_per_sample)2525 static void planar_to_interleaved( uint8_t *dest, AVFrame *src, int samples, int channels, int bytes_per_sample )
2526 {
2527 	int s, c;
2528 	for ( s = 0; s < samples; s++ )
2529 	{
2530 		for ( c = 0; c < channels; c++ )
2531 		{
2532 			if ( c < AV_NUM_DATA_POINTERS )
2533 				memcpy( dest, &src->data[c][s * bytes_per_sample], bytes_per_sample );
2534 			dest += bytes_per_sample;
2535 		}
2536 	}
2537 }
2538 
decode_audio(producer_avformat self,int * ignore,const AVPacket * pkt,int samples,double timecode,double fps)2539 static int decode_audio( producer_avformat self, int *ignore, const AVPacket *pkt, int samples, double timecode, double fps )
2540 {
2541 	// Fetch the audio_format
2542 	AVFormatContext *context = self->audio_format;
2543 
2544 	// Get the current stream index
2545 	int index = pkt->stream_index;
2546 
2547 	// Get codec context
2548 	AVCodecContext *codec_context = self->audio_codec[ index ];
2549 
2550 	// Obtain the audio buffers
2551 	uint8_t *audio_buffer = self->audio_buffer[ index ];
2552 
2553 	int channels = codec_context->channels;
2554 	int audio_used = self->audio_used[ index ];
2555 	int ret = 0;
2556 	int discarded = 1;
2557 	int sizeof_sample = sample_bytes( codec_context );
2558 
2559 	// Decode the audio
2560 	if ( !self->audio_frame )
2561 		self->audio_frame = av_frame_alloc();
2562 	else
2563 		av_frame_unref( self->audio_frame );
2564 	int error = avcodec_send_packet(codec_context, pkt);
2565 	mlt_log_debug(MLT_PRODUCER_SERVICE(self->parent), "decoded audio packet with size %d => %d\n", pkt->size, error);
2566 	if (!ignore_send_packet_result(error)) {
2567 		mlt_log_warning(MLT_PRODUCER_SERVICE(self->parent), "audio avcodec_send_packet failed with %d\n", error);
2568 	} else while (!error) {
2569 		error = avcodec_receive_frame(codec_context, self->audio_frame);
2570 		if (error) {
2571 			if (error != AVERROR(EAGAIN)) {
2572 				mlt_log_warning(MLT_PRODUCER_SERVICE(self->parent), "audio decoding error %d\n", error);
2573 			}
2574 		} else {
2575 			// Figure out how many samples will be needed after resampling
2576 			int convert_samples = self->audio_frame->nb_samples;
2577 			channels = codec_context->channels;
2578 			ret += convert_samples * channels * sizeof_sample;
2579 
2580 			// Resize audio buffer to prevent overflow
2581 			if ( ( audio_used + convert_samples ) * channels * sizeof_sample > self->audio_buffer_size[ index ] )
2582 			{
2583 				self->audio_buffer_size[ index ] = ( audio_used + convert_samples * 2 ) * channels * sizeof_sample;
2584 				audio_buffer = self->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, self->audio_buffer_size[ index ] );
2585 			}
2586 			uint8_t *dest = &audio_buffer[ audio_used * channels * sizeof_sample ];
2587 			switch ( codec_context->sample_fmt )
2588 			{
2589 			case AV_SAMPLE_FMT_U8P:
2590 			case AV_SAMPLE_FMT_S16P:
2591 			case AV_SAMPLE_FMT_S32P:
2592 			case AV_SAMPLE_FMT_FLTP:
2593 				planar_to_interleaved( dest, self->audio_frame, convert_samples, channels, sizeof_sample );
2594 				break;
2595 			default: {
2596 				int data_size = av_samples_get_buffer_size( NULL, channels,
2597 					self->audio_frame->nb_samples, codec_context->sample_fmt, 1 );
2598 				// Straight copy to audio buffer
2599 				memcpy( dest, self->audio_frame->data[0], data_size );
2600 				}
2601 			}
2602 			audio_used += convert_samples;
2603 			discarded = 0;
2604 		}
2605 	}
2606 
2607 	// Handle ignore
2608 	if ( *ignore > 0 && audio_used )
2609 	{
2610 		int n = FFMIN( audio_used, *ignore );
2611 		*ignore -= n;
2612 		audio_used -= n;
2613 		memmove( audio_buffer, &audio_buffer[ n * channels * sizeof_sample ],
2614 				 audio_used * channels * sizeof_sample );
2615 	}
2616 
2617 	// If we're behind, ignore this packet
2618 	// Skip this on non-seekable, audio-only inputs.
2619 	if ( !discarded && pkt->pts >= 0 && ( self->seekable || self->video_format ) && *ignore == 0 && audio_used > samples / 2 )
2620 	{
2621 		int64_t pts = pkt->pts;
2622 		if ( self->first_pts != AV_NOPTS_VALUE )
2623 			pts -= self->first_pts;
2624 		else if ( context->start_time != AV_NOPTS_VALUE && self->video_index != -1 )
2625 			pts -= context->start_time;
2626 		double timebase = av_q2d( context->streams[ index ]->time_base );
2627 		int64_t int_position = llrint( timebase * pts * fps );
2628 		int64_t req_position = llrint( timecode * fps );
2629 		int64_t req_pts =      llrint( timecode / timebase );
2630 
2631 		mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent),
2632 			"A pkt.pts %"PRId64" pkt->dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n",
2633 			pkt->pts, pkt->dts, req_position, self->current_position, int_position );
2634 
2635 		if ( self->seekable || int_position > 0 )
2636 		{
2637 			if ( req_position > int_position ) {
2638 				// We are behind, so skip some
2639 				*ignore = lrint( timebase * (req_pts - pts) * codec_context->sample_rate );
2640 			} else if ( self->audio_index != INT_MAX && int_position > req_position + 2 && !self->is_audio_synchronizing ) {
2641 				// We are ahead, so seek backwards some more.
2642 				// Supply -1 as the position to defeat the checks needed by for the other
2643 				// call to seek_audio() at the beginning of producer_get_audio(). Otherwise,
2644 				// more often than not, req_position will equal audio_expected.
2645 				seek_audio( self, -1, timecode - 1.0 );
2646 				self->is_audio_synchronizing = 1;
2647 			}
2648 		}
2649 
2650 		// Cancel the find_first_pts() in seek_audio()
2651 		if ( self->video_index == -1 && self->last_position == POSITION_INITIAL )
2652 			self->last_position = int_position;
2653 	}
2654 
2655 	self->audio_used[ index ] = audio_used;
2656 
2657 	return ret;
2658 }
2659 
2660 /** Get the audio from a frame.
2661 */
producer_get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)2662 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
2663 {
2664 	// Get the producer
2665 	producer_avformat self = mlt_frame_pop_audio( frame );
2666 
2667 	pthread_mutex_lock( &self->audio_mutex );
2668 
2669 	// Obtain the frame number of this frame
2670 	mlt_position position = mlt_frame_original_position( frame );
2671 
2672 	// Calculate the real time code
2673 	double real_timecode = producer_time_of_frame( self->parent, position );
2674 
2675 	// Get the producer fps
2676 	double fps = mlt_producer_get_fps( self->parent );
2677 	if ( mlt_properties_get( MLT_FRAME_PROPERTIES(frame), "producer_consumer_fps" ) )
2678 		fps = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "producer_consumer_fps" );
2679 
2680 	// Number of frames to ignore (for ffwd)
2681 	int ignore[ MAX_AUDIO_STREAMS ] = { 0 };
2682 
2683 	// Flag for paused (silence)
2684 	double timecode = self->audio_expected > 0 ? real_timecode : FFMAX(real_timecode - 0.25, 0.0);
2685 	int paused = seek_audio( self, position, timecode );
2686 
2687 	// Initialize ignore for all streams from the seek return value
2688 	int i = MAX_AUDIO_STREAMS;
2689 	while ( i-- )
2690 		ignore[i] = ignore[0];
2691 
2692 	// Fetch the audio_format
2693 	AVFormatContext *context = self->audio_format;
2694 	if ( !context )
2695 		goto exit_get_audio;
2696 
2697 	int sizeof_sample = sizeof( int16_t );
2698 
2699 	// Determine the tracks to use
2700 	int index = self->audio_index;
2701 	int index_max = self->audio_index + 1;
2702 	if ( self->audio_index == INT_MAX )
2703 	{
2704 		index = 0;
2705 		index_max = FFMIN( MAX_AUDIO_STREAMS, context->nb_streams );
2706 		*channels = self->total_channels;
2707 		*samples = mlt_audio_calculate_frame_samples( fps, self->max_frequency, position );
2708 		*frequency = self->max_frequency;
2709 	}
2710 
2711 	// Initialize the buffers
2712 	for ( ; index < index_max && index < MAX_AUDIO_STREAMS; index++ )
2713 	{
2714 		// Get codec context
2715 		AVCodecContext *codec_context = self->audio_codec[ index ];
2716 
2717 		if ( codec_context && !self->audio_buffer[ index ] )
2718 		{
2719 			if ( self->audio_index != INT_MAX && !mlt_properties_get( MLT_PRODUCER_PROPERTIES(self->parent), "request_channel_layout" ) )
2720 				codec_context->request_channel_layout = av_get_default_channel_layout( *channels );
2721 			sizeof_sample = sample_bytes( codec_context );
2722 
2723 			// Check for audio buffer and create if necessary
2724 			self->audio_buffer_size[ index ] = MAX_AUDIO_FRAME_SIZE * sizeof_sample;
2725 			self->audio_buffer[ index ] = mlt_pool_alloc( self->audio_buffer_size[ index ] );
2726 
2727 			// Check for decoder buffer and create if necessary
2728 			self->decode_buffer[ index ] = av_malloc( self->audio_buffer_size[ index ] );
2729 		}
2730 	}
2731 
2732 	// Get the audio if required
2733 	if ( !paused && *frequency > 0 )
2734 	{
2735 		int ret	= 0;
2736 		int got_audio = 0;
2737 		AVPacket pkt;
2738 		mlt_channel_layout layout = mlt_channel_auto;
2739 
2740 		av_init_packet( &pkt );
2741 
2742 		// Caller requested number samples based on requested sample rate.
2743 		if ( self->audio_index != INT_MAX )
2744 			*samples = mlt_audio_calculate_frame_samples( fps, self->audio_codec[ self->audio_index ]->sample_rate, position );
2745 
2746 		while ( ret >= 0 && !got_audio )
2747 		{
2748 			// Check if the buffer already contains the samples required
2749 			if ( self->audio_index != INT_MAX &&
2750 				 self->audio_used[ self->audio_index ] >= *samples &&
2751 				 ignore[ self->audio_index ] == 0 )
2752 			{
2753 				got_audio = 1;
2754 				break;
2755 			}
2756 			else if ( self->audio_index == INT_MAX )
2757 			{
2758 				// Check if there is enough audio for all streams
2759 				got_audio = 1;
2760 				for ( index = 0; got_audio && index < index_max; index++ )
2761 					if ( ( self->audio_codec[ index ] && self->audio_used[ index ] < *samples ) || ignore[ index ] )
2762 						got_audio = 0;
2763 				if ( got_audio )
2764 					break;
2765 			}
2766 
2767 			// Read a packet
2768 			pthread_mutex_lock( &self->packets_mutex );
2769 			if ( mlt_deque_count( self->apackets ) )
2770 			{
2771 				AVPacket *tmp = (AVPacket*) mlt_deque_pop_front( self->apackets );
2772 				av_packet_ref( &pkt, tmp );
2773 				av_packet_free( &tmp );
2774 			}
2775 			else
2776 			{
2777 				ret = av_read_frame( context, &pkt );
2778 				if ( ret >= 0 && !self->seekable && pkt.stream_index == self->video_index )
2779 				{
2780 					mlt_deque_push_back( self->vpackets, av_packet_clone(&pkt) );
2781 				}
2782 				else if ( ret < 0 )
2783 				{
2784 					mlt_producer producer = self->parent;
2785 					mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2786 					if ( ret != AVERROR_EOF )
2787 						mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "av_read_frame returned error %d inside get_audio\n", ret );
2788 					if ( !self->seekable && mlt_properties_get_int( properties, "reconnect" ) )
2789 					{
2790 						// Try to reconnect to live sources by closing context and codecs,
2791 						// and letting next call to get_frame() reopen.
2792 						prepare_reopen( self );
2793 						pthread_mutex_unlock( &self->packets_mutex );
2794 						goto exit_get_audio;
2795 					}
2796 					if ( !self->seekable && mlt_properties_get_int( properties, "exit_on_disconnect" ) )
2797 					{
2798 						mlt_log_fatal( MLT_PRODUCER_SERVICE(producer), "Exiting with error due to disconnected source.\n" );
2799 						exit( EXIT_FAILURE );
2800 					}
2801 				}
2802 			}
2803 			pthread_mutex_unlock( &self->packets_mutex );
2804 
2805 			// We only deal with audio from the selected audio index
2806 			index = pkt.stream_index;
2807 			if ( index < MAX_AUDIO_STREAMS && ret >= 0 && pkt.data && pkt.size > 0 && ( index == self->audio_index ||
2808 				 ( self->audio_index == INT_MAX && context->streams[ index ]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ) ) )
2809 			{
2810 				ret = decode_audio( self, &ignore[index], &pkt, *samples, real_timecode, fps );
2811 			}
2812 
2813 			if ( self->seekable || index != self->video_index )
2814 				av_packet_unref( &pkt );
2815 		}
2816 		self->is_audio_synchronizing = 0;
2817 
2818 		// Set some additional return values
2819 		*format = mlt_audio_s16;
2820 		if ( self->audio_index != INT_MAX )
2821 		{
2822 			index = self->audio_index;
2823 			*channels = self->audio_codec[ index ]->channels;
2824 			*frequency = self->audio_codec[ index ]->sample_rate;
2825 			*format = pick_audio_format( self->audio_codec[ index ]->sample_fmt );
2826 			sizeof_sample = sample_bytes( self->audio_codec[ index ] );
2827 			if( self->audio_codec[ index ]->channel_layout == 0 )
2828 				layout = av_channel_layout_to_mlt( av_get_default_channel_layout( self->audio_codec[ index ]->channels ) );
2829 			else
2830 				layout = av_channel_layout_to_mlt( self->audio_codec[ index ]->channel_layout );
2831 		}
2832 		else if ( self->audio_index == INT_MAX )
2833 		{
2834 			layout = mlt_channel_independent;
2835 			for ( index = 0; index < index_max; index++ )
2836 				if ( self->audio_codec[ index ] )
2837 				{
2838 					// XXX: This only works if all audio tracks have the same sample format.
2839 					*format = pick_audio_format( self->audio_codec[ index ]->sample_fmt );
2840 					sizeof_sample = sample_bytes( self->audio_codec[ index ] );
2841 					break;
2842 				}
2843 		}
2844 		mlt_properties_set( MLT_FRAME_PROPERTIES(frame), "channel_layout", mlt_audio_channel_layout_name( layout ) );
2845 
2846 		// Allocate and set the frame's audio buffer
2847 		int size = mlt_audio_format_size( *format, *samples, *channels );
2848 		*buffer = mlt_pool_alloc( size );
2849 		mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
2850 
2851 		// Interleave tracks if audio_index=all
2852 		if ( self->audio_index == INT_MAX )
2853 		{
2854 			uint8_t *dest = *buffer;
2855 			int i;
2856 			for ( i = 0; i < *samples; i++ )
2857 			{
2858 				for ( index = 0; index < index_max; index++ )
2859 				if ( self->audio_codec[ index ] )
2860 				{
2861 					int current_channels = self->audio_codec[ index ]->channels;
2862 					uint8_t *src = self->audio_buffer[ index ] + i * current_channels * sizeof_sample;
2863 					memcpy( dest, src, current_channels * sizeof_sample );
2864 					dest += current_channels * sizeof_sample;
2865 				}
2866 			}
2867 			for ( index = 0; index < index_max; index++ )
2868 			if ( self->audio_codec[ index ] && self->audio_used[ index ] >= *samples )
2869 			{
2870 				int current_channels = self->audio_codec[ index ]->channels;
2871 				uint8_t *src = self->audio_buffer[ index ] + *samples * current_channels * sizeof_sample;
2872 				self->audio_used[index] -= *samples;
2873 				memmove( self->audio_buffer[ index ], src, self->audio_used[ index ] * current_channels * sizeof_sample );
2874 			}
2875 		}
2876 		// Copy a single track to the output buffer
2877 		else
2878 		{
2879 			index = self->audio_index;
2880 			uint8_t silence = *format == mlt_audio_u8 ? 0x80 : 0;
2881 
2882 			// Now handle the audio if we have enough
2883 			if ( self->audio_used[ index ] > 0 )
2884 			{
2885 				uint8_t *src = self->audio_buffer[ index ];
2886 				// copy samples from audio_buffer
2887 				size = self->audio_used[ index ] < *samples ? self->audio_used[ index ] : *samples;
2888 				memcpy( *buffer, src, size * *channels * sizeof_sample );
2889 				// supply the remaining requested samples as silence
2890 				if ( *samples > self->audio_used[ index ] )
2891 					memset( *buffer + size * *channels * sizeof_sample, silence, ( *samples - self->audio_used[ index ] ) * *channels * sizeof_sample );
2892 				// reposition the samples within audio_buffer
2893 				self->audio_used[ index ] -= size;
2894 				memmove( src, src + size * *channels * sizeof_sample, self->audio_used[ index ] * *channels * sizeof_sample );
2895 			}
2896 			else
2897 			{
2898 				// Otherwise fill with silence
2899 				memset( *buffer, silence, *samples * *channels * sizeof_sample );
2900 			}
2901 		}
2902 	}
2903 	else
2904 	{
2905 exit_get_audio:
2906 		// Get silence and don't touch the context
2907 		mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
2908 	}
2909 
2910 	// Regardless of speed (other than paused), we expect to get the next frame
2911 	if ( !paused )
2912 		self->audio_expected = position + 1;
2913 
2914 	pthread_mutex_unlock( &self->audio_mutex );
2915 
2916 	return 0;
2917 }
2918 
2919 /** Initialize the audio codec context.
2920 */
2921 
audio_codec_init(producer_avformat self,int index,mlt_properties properties)2922 static int audio_codec_init( producer_avformat self, int index, mlt_properties properties )
2923 {
2924 	// Initialise the codec if necessary
2925 	if ( !self->audio_codec[ index ] )
2926 	{
2927 		// Get codec context
2928 		AVCodecContext *codec_context = self->audio_format->streams[index]->codec;
2929 
2930 		// Find the codec
2931 		AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
2932 		if ( mlt_properties_get( properties, "acodec" ) )
2933 		{
2934 			if ( !( codec = avcodec_find_decoder_by_name( mlt_properties_get( properties, "acodec" ) ) ) )
2935 				codec = avcodec_find_decoder( codec_context->codec_id );
2936 		}
2937 
2938 		// If we don't have a codec and we can't initialise it, we can't do much more...
2939 		pthread_mutex_lock( &self->open_mutex );
2940 		if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
2941 		{
2942 			// Now store the codec with its destructor
2943 			if ( self->audio_codec[ index ] )
2944 				avcodec_close( self->audio_codec[ index ] );
2945 			self->audio_codec[ index ] = codec_context;
2946 			self->audio_index = index;
2947 		}
2948 		else
2949 		{
2950 			// Remember that we can't use self later
2951 			self->audio_index = -1;
2952 		}
2953 		pthread_mutex_unlock( &self->open_mutex );
2954 
2955 		// Process properties as AVOptions
2956 		apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2957 		if ( codec && codec->priv_class && codec_context->priv_data )
2958 			apply_properties( codec_context->priv_data, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2959 	}
2960 	return self->audio_codec[ index ] && self->audio_index > -1;
2961 }
2962 
2963 /** Set up audio handling.
2964 */
2965 
producer_set_up_audio(producer_avformat self,mlt_frame frame)2966 static void producer_set_up_audio( producer_avformat self, mlt_frame frame )
2967 {
2968 	// Get the producer
2969 	mlt_producer producer = self->parent;
2970 
2971 	// Get the properties
2972 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2973 
2974 	// Fetch the audio format context
2975 	AVFormatContext *context = self->audio_format;
2976 
2977 	mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2978 
2979 	// Get the audio_index
2980 	int index = mlt_properties_get_int( properties, "audio_index" );
2981 
2982 	// Handle all audio tracks
2983 	if ( self->audio_index > -1 &&
2984 	     mlt_properties_get( properties, "audio_index" ) &&
2985 	     !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
2986 		index = INT_MAX;
2987 
2988 	// Reopen the file if necessary
2989 	if ( !context && self->audio_index > -1 && index > -1 )
2990 	{
2991 		producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2992 			mlt_properties_get( properties, "resource" ), 1, 0 );
2993 		context = self->audio_format;
2994 	}
2995 
2996 	// Exception handling for audio_index
2997 	if ( context && index >= (int) context->nb_streams && index < INT_MAX )
2998 	{
2999 		for ( index = context->nb_streams - 1;
3000 			  index >= 0 && context->streams[ index ]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO;
3001 			  index-- );
3002 		mlt_properties_set_int( properties, "audio_index", index );
3003 	}
3004 	if ( context && index > -1 && index < INT_MAX &&
3005 		 context->streams[ index ]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO )
3006 	{
3007 		index = self->audio_index;
3008 		mlt_properties_set_int( properties, "audio_index", index );
3009 	}
3010 	if ( context && index > -1 && index < INT_MAX &&
3011 		 pick_audio_format( context->streams[ index ]->codecpar->format ) == mlt_audio_none )
3012 	{
3013 		index = -1;
3014 	}
3015 
3016 	// Update the audio properties if the index changed
3017 	if ( context && self->audio_index > -1 && index != self->audio_index )
3018 	{
3019 		pthread_mutex_lock( &self->open_mutex );
3020 		unsigned i = 0;
3021 		int index_max = FFMIN( MAX_AUDIO_STREAMS, context->nb_streams );
3022 		for (i = 0; i < index_max; i++) {
3023 			if (self->audio_codec[i]) {
3024 				avcodec_close(self->audio_codec[i]);
3025 				self->audio_codec[i] = NULL;
3026 			}
3027 		}
3028 		pthread_mutex_unlock( &self->open_mutex );
3029 	}
3030 
3031 	// Get the codec(s)
3032 	if ( context && index == INT_MAX )
3033 	{
3034 		unsigned int index;
3035 		mlt_properties_set_int( frame_properties, "audio_frequency", self->max_frequency );
3036 		mlt_properties_set_int( frame_properties, "audio_channels", self->total_channels );
3037 		for ( index = 0; index < context->nb_streams && index < MAX_AUDIO_STREAMS; index++ )
3038 		{
3039 			if ( context->streams[ index ]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
3040 				audio_codec_init( self, index, properties );
3041 		}
3042 		self->audio_index = INT_MAX;
3043 	}
3044 	else if ( context && index > -1 && index < MAX_AUDIO_STREAMS &&
3045 		audio_codec_init( self, index, properties ) )
3046 	{
3047 		mlt_properties_set_int( frame_properties, "audio_frequency", self->audio_codec[ index ]->sample_rate );
3048 		mlt_properties_set_int( frame_properties, "audio_channels", self->audio_codec[ index ]->channels );
3049 	}
3050 	if ( context && index > -1 )
3051 	{
3052 		// Add our audio operation
3053 		mlt_frame_push_audio( frame, self );
3054 		mlt_frame_push_audio( frame, producer_get_audio );
3055 	}
3056 }
3057 
3058 /** Our get frame implementation.
3059 */
3060 
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)3061 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
3062 {
3063 	// Access the private data
3064 	(void) index; // unused
3065 	mlt_service service = MLT_PRODUCER_SERVICE( producer );
3066 	mlt_cache_item cache_item = mlt_service_cache_get( service, "producer_avformat" );
3067 	producer_avformat self = mlt_cache_item_data( cache_item, NULL );
3068 
3069 	// If cache miss
3070 	if ( !self )
3071 	{
3072 		self = calloc( 1, sizeof( struct producer_avformat_s ) );
3073 		producer->child = self;
3074 		self->parent = producer;
3075 		mlt_service_cache_put( service, "producer_avformat", self, 0, (mlt_destructor) producer_avformat_close );
3076 		cache_item = mlt_service_cache_get( service, "producer_avformat" );
3077 	}
3078 
3079 	// Create an empty frame
3080 	*frame = mlt_frame_init( service);
3081 
3082 	if ( *frame )
3083 	{
3084 		mlt_properties_set_data( MLT_FRAME_PROPERTIES(*frame), "avformat_cache", cache_item, 0, (mlt_destructor) mlt_cache_item_close, NULL );
3085 	}
3086 	else
3087 	{
3088 		mlt_cache_item_close( cache_item );
3089 		return 1;
3090 	}
3091 
3092 	// Update timecode on the frame we're creating
3093 	mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
3094 
3095 	// Set up the video
3096 	producer_set_up_video( self, *frame );
3097 
3098 	// Set up the audio
3099 	producer_set_up_audio( self, *frame );
3100 
3101 	// Set the position of this producer
3102 	mlt_position position = mlt_producer_frame( producer );
3103 	mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "original_position", position );
3104 
3105 	// Calculate the next timecode
3106 	mlt_producer_prepare_next( producer );
3107 
3108 	return 0;
3109 }
3110 
producer_avformat_close(producer_avformat self)3111 static void producer_avformat_close( producer_avformat self )
3112 {
3113 	mlt_log_debug( NULL, "producer_avformat_close\n" );
3114 
3115 	// Cleanup av contexts
3116 	av_packet_unref( &self->pkt );
3117 	av_frame_free( &self->video_frame );
3118 	av_frame_free( &self->audio_frame );
3119 
3120 #if USE_HWACCEL
3121 	av_buffer_unref( &self->hwaccel.device_ctx );
3122 #endif
3123 
3124 	if ( self->is_mutex_init )
3125 		pthread_mutex_lock( &self->open_mutex );
3126 	int i;
3127 	for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
3128 	{
3129 		mlt_pool_release( self->audio_buffer[i] );
3130 		av_free( self->decode_buffer[i] );
3131 		if ( self->audio_codec[i] )
3132 			avcodec_close( self->audio_codec[i] );
3133 		self->audio_codec[i] = NULL;
3134 	}
3135 	if ( self->video_codec )
3136 		avcodec_close( self->video_codec );
3137 	self->video_codec = NULL;
3138 	// Close the file
3139 	if ( self->dummy_context )
3140 		avformat_close_input( &self->dummy_context );
3141 	if ( self->seekable && self->audio_format )
3142 		avformat_close_input( &self->audio_format );
3143 	if ( self->video_format )
3144 		avformat_close_input( &self->video_format );
3145 	if ( self->is_mutex_init )
3146 		pthread_mutex_unlock( &self->open_mutex );
3147 #ifdef AVFILTER
3148 	avfilter_graph_free(&self->vfilter_graph);
3149 #endif
3150 
3151 	// Cleanup caches.
3152 	mlt_cache_close( self->image_cache );
3153 	if ( self->last_good_frame )
3154 		mlt_frame_close( self->last_good_frame );
3155 
3156 	// Cleanup the mutexes
3157 	if ( self->is_mutex_init )
3158 	{
3159 		pthread_mutex_destroy( &self->audio_mutex );
3160 		pthread_mutex_destroy( &self->video_mutex );
3161 		pthread_mutex_destroy( &self->packets_mutex );
3162 		pthread_mutex_destroy( &self->open_mutex );
3163 	}
3164 
3165 	// Cleanup the packet queues
3166 	AVPacket *pkt;
3167 	if ( self->apackets )
3168 	{
3169 		while ( ( pkt = mlt_deque_pop_back( self->apackets ) ) )
3170 		{
3171 			av_packet_free( &pkt );
3172 		}
3173 		mlt_deque_close( self->apackets );
3174 		self->apackets = NULL;
3175 	}
3176 	if ( self->vpackets )
3177 	{
3178 		while ( ( pkt = mlt_deque_pop_back( self->vpackets ) ) )
3179 		{
3180 			av_packet_free( &pkt );
3181 		}
3182 		mlt_deque_close( self->vpackets );
3183 		self->vpackets = NULL;
3184 	}
3185 
3186 	free( self );
3187 }
3188 
producer_close(mlt_producer parent)3189 static void producer_close( mlt_producer parent )
3190 {
3191 	// Remove this instance from the cache
3192 	mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
3193 
3194 	// Close the parent
3195 	parent->close = NULL;
3196 	mlt_producer_close( parent );
3197 
3198 	// Free the memory
3199 	free( parent );
3200 }
3201