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