xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp (revision d579eb9efe82919385bd0e18d9c26baa6d5d46bf)
1 /*
2  * Copyright 2009-2010, Stephan Aßmus <superstippi@gmx.de>
3  * Copyright 2014, Colin Günther <coling@gmx.de>
4  * Copyright 2018, Dario Casalinuovo
5  * All rights reserved. Distributed under the terms of the GNU L-GPL license.
6  */
7 
8 #include "AVFormatReader.h"
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 #include <new>
15 
16 #include <AutoDeleter.h>
17 #include <Autolock.h>
18 #include <ByteOrder.h>
19 #include <MediaIO.h>
20 #include <MediaDefs.h>
21 #include <MediaFormats.h>
22 #include <MimeType.h>
23 
24 extern "C" {
25 	#include "avcodec.h"
26 	#include "avformat.h"
27 }
28 
29 #include "DemuxerTable.h"
30 #include "gfx_util.h"
31 #include "Utilities.h"
32 
33 
34 //#define TRACE_AVFORMAT_READER
35 #ifdef TRACE_AVFORMAT_READER
36 #	define TRACE printf
37 #	define TRACE_IO(a...)
38 #	define TRACE_SEEK(a...) printf(a)
39 #	define TRACE_FIND(a...)
40 #	define TRACE_PACKET(a...)
41 #else
42 #	define TRACE(a...)
43 #	define TRACE_IO(a...)
44 #	define TRACE_SEEK(a...)
45 #	define TRACE_FIND(a...)
46 #	define TRACE_PACKET(a...)
47 #endif
48 
49 #define ERROR(a...) fprintf(stderr, a)
50 
51 // Compatibility with old ffmpeg 4.x, where the getters didn't exist yet
52 #if LIBAVCODEC_VERSION_MAJOR < 60
53 #define avformat_index_get_entry(stream, index) (&(stream)->index_entries[(index)])
54 #define avformat_index_get_entries_count(stream) ((stream)->nb_index_entries)
55 #endif
56 
57 
58 static uint32
59 avformat_to_beos_byte_order(AVSampleFormat format)
60 {
61 	// TODO: Huh?
62 	return B_MEDIA_HOST_ENDIAN;
63 }
64 
65 
66 static void
67 avdictionary_to_message(AVDictionary* dictionary, BMessage* message)
68 {
69 	if (dictionary == NULL)
70 		return;
71 
72 	AVDictionaryEntry* entry = NULL;
73 	while ((entry = av_dict_get(dictionary, "", entry,
74 		AV_DICT_IGNORE_SUFFIX))) {
75 		// convert entry keys into something more meaningful using the names from
76 		// id3v2.c
77 		if (strcmp(entry->key, "TALB") == 0 || strcmp(entry->key, "TAL") == 0)
78 			message->AddString("album", entry->value);
79 		else if (strcmp(entry->key, "TCOM") == 0)
80 			message->AddString("composer", entry->value);
81 		else if (strcmp(entry->key, "TCON") == 0 || strcmp(entry->key, "TCO") == 0)
82 			message->AddString("genre", entry->value);
83 		else if (strcmp(entry->key, "TCOP") == 0)
84 			message->AddString("copyright", entry->value);
85 		else if (strcmp(entry->key, "TDRL") == 0 || strcmp(entry->key, "TDRC") == 0)
86 			message->AddString("date", entry->value);
87 		else if (strcmp(entry->key, "TENC") == 0 || strcmp(entry->key, "TEN") == 0)
88 			message->AddString("encoded_by", entry->value);
89 		else if (strcmp(entry->key, "TIT2") == 0 || strcmp(entry->key, "TT2") == 0)
90 			message->AddString("title", entry->value);
91 		else if (strcmp(entry->key, "TLAN") == 0)
92 			message->AddString("language", entry->value);
93 		else if (strcmp(entry->key, "TPE1") == 0 || strcmp(entry->key, "TP1") == 0)
94 			message->AddString("artist", entry->value);
95 		else if (strcmp(entry->key, "TPE2") == 0 || strcmp(entry->key, "TP2") == 0)
96 			message->AddString("album_artist", entry->value);
97 		else if (strcmp(entry->key, "TPE3") == 0 || strcmp(entry->key, "TP3") == 0)
98 			message->AddString("performer", entry->value);
99 		else if (strcmp(entry->key, "TPOS") == 0)
100 			message->AddString("disc", entry->value);
101 		else if (strcmp(entry->key, "TPUB") == 0)
102 			message->AddString("publisher", entry->value);
103 		else if (strcmp(entry->key, "TRCK") == 0 || strcmp(entry->key, "TRK") == 0)
104 			message->AddString("track", entry->value);
105 		else if (strcmp(entry->key, "TSOA") == 0)
106 			message->AddString("album-sort", entry->value);
107 		else if (strcmp(entry->key, "TSOP") == 0)
108 			message->AddString("artist-sort", entry->value);
109 		else if (strcmp(entry->key, "TSOT") == 0)
110 			message->AddString("title-sort", entry->value);
111 		else if (strcmp(entry->key, "TSSE") == 0)
112 			message->AddString("encoder", entry->value);
113 		else if (strcmp(entry->key, "TYER") == 0)
114 			message->AddString("year", entry->value);
115 		else
116 			message->AddString(entry->key, entry->value);
117 	}
118 }
119 
120 
121 // #pragma mark - StreamBase
122 
123 
124 class StreamBase {
125 public:
126 								StreamBase(BMediaIO* source,
127 									BLocker* sourceLock, BLocker* streamLock);
128 	virtual						~StreamBase();
129 
130 	// Init an indivual AVFormatContext
131 			status_t			Open();
132 
133 	// Setup this stream to point to the AVStream at the given streamIndex.
134 	virtual	status_t			Init(int32 streamIndex);
135 
136 	inline	const AVFormatContext* Context() const
137 									{ return fContext; }
138 			int32				Index() const;
139 			int32				CountStreams() const;
140 			int32				StreamIndexFor(int32 virtualIndex) const;
141 	inline	int32				VirtualIndex() const
142 									{ return fVirtualIndex; }
143 
144 			double				FrameRate() const;
145 			bigtime_t			Duration() const;
146 
147 	virtual	status_t			Seek(uint32 flags, int64* frame,
148 									bigtime_t* time);
149 
150 			status_t			GetNextChunk(const void** chunkBuffer,
151 									size_t* chunkSize,
152 									media_header* mediaHeader);
153 
154 protected:
155 	// I/O hooks for libavformat, cookie will be a Stream instance.
156 	// Since multiple StreamCookies use the same BMediaIO source, they
157 	// maintain the position individually, and may need to seek the source
158 	// if it does not match anymore in _Read().
159 	static	int					_Read(void* cookie, uint8* buffer,
160 									int bufferSize);
161 	static	off_t				_Seek(void* cookie, off_t offset, int whence);
162 
163 			status_t			_NextPacket(bool reuse);
164 
165 			int64_t				_ConvertToStreamTimeBase(bigtime_t time) const;
166 			bigtime_t			_ConvertFromStreamTimeBase(int64_t time) const;
167 
168 protected:
169 			BMediaIO*			fSource;
170 			off_t				fPosition;
171 			// Since different threads may read from the source,
172 			// we need to protect the file position and I/O by a lock.
173 			BLocker*			fSourceLock;
174 
175 			BLocker*			fStreamLock;
176 
177 			AVFormatContext*	fContext;
178 			AVStream*			fStream;
179 			int32				fVirtualIndex;
180 
181 			media_format		fFormat;
182 
183 			AVIOContext*		fIOContext;
184 
185 			AVPacket			fPacket;
186 			bool				fReusePacket;
187 
188 			bool				fSeekByBytes;
189 			bool				fStreamBuildsIndexWhileReading;
190 };
191 
192 
193 StreamBase::StreamBase(BMediaIO* source, BLocker* sourceLock,
194 		BLocker* streamLock)
195 	:
196 	fSource(source),
197 	fPosition(0),
198 	fSourceLock(sourceLock),
199 
200 	fStreamLock(streamLock),
201 
202 	fContext(NULL),
203 	fStream(NULL),
204 	fVirtualIndex(-1),
205 	fIOContext(NULL),
206 
207 	fReusePacket(false),
208 
209 	fSeekByBytes(false),
210 	fStreamBuildsIndexWhileReading(false)
211 {
212 	// NOTE: Don't use streamLock here, it may not yet be initialized!
213 
214 	av_new_packet(&fPacket, 0);
215 	fFormat.Clear();
216 }
217 
218 
219 StreamBase::~StreamBase()
220 {
221 	avformat_close_input(&fContext);
222 	av_packet_unref(&fPacket);
223 	if (fIOContext != NULL)
224 		av_free(fIOContext->buffer);
225 	av_free(fIOContext);
226 }
227 
228 
229 status_t
230 StreamBase::Open()
231 {
232 	BAutolock _(fStreamLock);
233 
234 	// Init probing data
235 	size_t bufferSize = 32768;
236 	uint8* buffer = static_cast<uint8*>(av_malloc(bufferSize));
237 	if (buffer == NULL)
238 		return B_NO_MEMORY;
239 
240 	// First try to identify the file using the MIME database, as ffmpeg
241 	// is not very good at this and relies on us to give it the file extension
242 	// as an hint.
243 	// For this we need some valid data in the buffer, the first 512 bytes
244 	// should do because our MIME sniffing never uses more.
245 	const char* extension = NULL;
246 	BMessage message;
247 	if (fSource->Read(buffer, 512) == 512) {
248 		BMimeType type;
249 		if (BMimeType::GuessMimeType(buffer, 512, &type) == B_OK) {
250 			if (type.GetFileExtensions(&message) == B_OK) {
251 				extension = message.FindString("extensions");
252 			}
253 		}
254 	}
255 
256 	// Allocate I/O context with buffer and hook functions, pass ourself as
257 	// cookie.
258 	memset(buffer, 0, bufferSize);
259 	fIOContext = avio_alloc_context(buffer, bufferSize, 0, this, _Read, 0,
260 		_Seek);
261 	if (fIOContext == NULL) {
262 		TRACE("StreamBase::Open() - avio_alloc_context() failed!\n");
263 		av_free(buffer);
264 		return B_ERROR;
265 	}
266 
267 	fContext = avformat_alloc_context();
268 	fContext->pb = fIOContext;
269 
270 	// Allocate our context and probe the input format
271 	if (avformat_open_input(&fContext, extension, NULL, NULL) < 0) {
272 		TRACE("StreamBase::Open() - avformat_open_input() failed!\n");
273 		// avformat_open_input() frees the context in case of failure
274 		fContext = NULL;
275 		av_free(fIOContext->buffer);
276 		av_free(fIOContext);
277 		fIOContext = NULL;
278 		return B_NOT_SUPPORTED;
279 	}
280 
281 	TRACE("StreamBase::Open() - "
282 		"avformat_open_input(): %s\n", fContext->iformat->name);
283 	TRACE("  flags:%s%s%s%s%s\n",
284 		(fContext->iformat->flags & AVFMT_GLOBALHEADER) ? " AVFMT_GLOBALHEADER" : "",
285 		(fContext->iformat->flags & AVFMT_NOTIMESTAMPS) ? " AVFMT_NOTIMESTAMPS" : "",
286 		(fContext->iformat->flags & AVFMT_GENERIC_INDEX) ? " AVFMT_GENERIC_INDEX" : "",
287 		(fContext->iformat->flags & AVFMT_TS_DISCONT) ? " AVFMT_TS_DISCONT" : "",
288 		(fContext->iformat->flags & AVFMT_VARIABLE_FPS) ? " AVFMT_VARIABLE_FPS" : ""
289 	);
290 
291 
292 	// Retrieve stream information
293 	if (avformat_find_stream_info(fContext, NULL) < 0) {
294 		TRACE("StreamBase::Open() - avformat_find_stream_info() failed!\n");
295 		return B_NOT_SUPPORTED;
296 	}
297 
298 	fSeekByBytes = (fContext->iformat->flags & AVFMT_TS_DISCONT) != 0;
299 	fStreamBuildsIndexWhileReading
300 		= (fContext->iformat->flags & AVFMT_GENERIC_INDEX) != 0
301 			|| fSeekByBytes;
302 
303 	TRACE("StreamBase::Open() - "
304 		"av_find_stream_info() success! Seeking by bytes: %d\n",
305 		fSeekByBytes);
306 
307 	return B_OK;
308 }
309 
310 
311 status_t
312 StreamBase::Init(int32 virtualIndex)
313 {
314 	BAutolock _(fStreamLock);
315 
316 	TRACE("StreamBase::Init(%ld)\n", virtualIndex);
317 
318 	if (fContext == NULL)
319 		return B_NO_INIT;
320 
321 	int32 streamIndex = StreamIndexFor(virtualIndex);
322 	if (streamIndex < 0) {
323 		TRACE("  bad stream index!\n");
324 		return B_BAD_INDEX;
325 	}
326 
327 	TRACE("  context stream index: %ld\n", streamIndex);
328 
329 	// We need to remember the virtual index so that
330 	// AVFormatReader::FreeCookie() can clear the correct stream entry.
331 	fVirtualIndex = virtualIndex;
332 
333 	// Make us point to the AVStream at streamIndex
334 	fStream = fContext->streams[streamIndex];
335 
336 // NOTE: Discarding other streams works for most, but not all containers,
337 // for example it does not work for the ASF demuxer. Since I don't know what
338 // other demuxer it breaks, let's just keep reading packets for unwanted
339 // streams, it just makes the _GetNextPacket() function slightly less
340 // efficient.
341 //	// Discard all other streams
342 //	for (unsigned i = 0; i < fContext->nb_streams; i++) {
343 //		if (i != (unsigned)streamIndex)
344 //			fContext->streams[i]->discard = AVDISCARD_ALL;
345 //	}
346 
347 	return B_OK;
348 }
349 
350 
351 int32
352 StreamBase::Index() const
353 {
354 	if (fStream != NULL)
355 		return fStream->index;
356 	return -1;
357 }
358 
359 
360 int32
361 StreamBase::CountStreams() const
362 {
363 	// Figure out the stream count. If the context has "AVPrograms", use
364 	// the first program (for now).
365 	// TODO: To support "programs" properly, the BMediaFile/Track API should
366 	// be extended accordingly. I guess programs are like TV channels in the
367 	// same satilite transport stream. Maybe call them "TrackGroups".
368 	if (fContext->nb_programs > 0) {
369 		// See libavformat/utils.c:dump_format()
370 		return fContext->programs[0]->nb_stream_indexes;
371 	}
372 	return fContext->nb_streams;
373 }
374 
375 
376 int32
377 StreamBase::StreamIndexFor(int32 virtualIndex) const
378 {
379 	// NOTE: See CountStreams()
380 	if (fContext->nb_programs > 0) {
381 		const AVProgram* program = fContext->programs[0];
382 		if (virtualIndex >= 0
383 			&& virtualIndex < (int32)program->nb_stream_indexes) {
384 			return program->stream_index[virtualIndex];
385 		}
386 	} else {
387 		if (virtualIndex >= 0 && virtualIndex < (int32)fContext->nb_streams)
388 			return virtualIndex;
389 	}
390 	return -1;
391 }
392 
393 
394 double
395 StreamBase::FrameRate() const
396 {
397 	// TODO: Find a way to always calculate a correct frame rate...
398 	double frameRate = 1.0;
399 	switch (fStream->codecpar->codec_type) {
400 		case AVMEDIA_TYPE_AUDIO:
401 			frameRate = (double)fStream->codecpar->sample_rate;
402 			break;
403 		case AVMEDIA_TYPE_VIDEO:
404 		{
405 			AVRational frameRateFrac = av_guess_frame_rate(NULL, fStream, NULL);
406 			if (frameRateFrac.den != 0 && frameRateFrac.num != 0)
407 				frameRate = av_q2d(frameRateFrac);
408 			else if (fStream->time_base.den != 0 && fStream->time_base.num != 0)
409 				frameRate = 1 / av_q2d(fStream->time_base);
410 
411 			// Catch the obviously wrong default framerate when ffmpeg cannot
412 			// guess anything because there are not two frames to compute a
413 			// framerate
414 			if (fStream->nb_frames < 2 && frameRate == 90000.0f)
415 				return 0.0f;
416 			break;
417 		}
418 		default:
419 			break;
420 	}
421 	if (frameRate <= 0.0)
422 		frameRate = 1.0;
423 	return frameRate;
424 }
425 
426 
427 bigtime_t
428 StreamBase::Duration() const
429 {
430 	// TODO: This is not working correctly for all stream types...
431 	// It seems that the calculations here are correct, because they work
432 	// for a couple of streams and are in line with the documentation, but
433 	// unfortunately, libavformat itself seems to set the time_base and
434 	// duration wrongly sometimes. :-(
435 
436 	int32 flags;
437 	fSource->GetFlags(&flags);
438 
439 	// "Mutable Size" (ie http streams) means we can't realistically compute
440 	// a duration. So don't let ffmpeg give a (wrong) estimate in this case.
441 	if ((flags & B_MEDIA_MUTABLE_SIZE) != 0)
442 		return 0;
443 
444 	if ((int64)fStream->duration != AV_NOPTS_VALUE)
445 		return _ConvertFromStreamTimeBase(fStream->duration);
446 	else if ((int64)fContext->duration != AV_NOPTS_VALUE)
447 		return (bigtime_t)fContext->duration;
448 
449 	return 0;
450 }
451 
452 
453 status_t
454 StreamBase::Seek(uint32 flags, int64* frame, bigtime_t* time)
455 {
456 	BAutolock _(fStreamLock);
457 
458 	if (fContext == NULL || fStream == NULL)
459 		return B_NO_INIT;
460 
461 	TRACE_SEEK("StreamBase::Seek(%ld,%s%s%s%s, %lld, "
462 		"%lld)\n", VirtualIndex(),
463 		(flags & B_MEDIA_SEEK_TO_FRAME) ? " B_MEDIA_SEEK_TO_FRAME" : "",
464 		(flags & B_MEDIA_SEEK_TO_TIME) ? " B_MEDIA_SEEK_TO_TIME" : "",
465 		(flags & B_MEDIA_SEEK_CLOSEST_BACKWARD)
466 			? " B_MEDIA_SEEK_CLOSEST_BACKWARD" : "",
467 		(flags & B_MEDIA_SEEK_CLOSEST_FORWARD)
468 			? " B_MEDIA_SEEK_CLOSEST_FORWARD" : "",
469 		*frame, *time);
470 
471 	double frameRate = FrameRate();
472 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
473 		// Seeking is always based on time, initialize it when client seeks
474 		// based on frame.
475 		*time = (bigtime_t)(*frame * 1000000.0 / frameRate + 0.5);
476 	}
477 
478 	int64_t timeStamp = *time;
479 
480 	int searchFlags = AVSEEK_FLAG_BACKWARD;
481 	if ((flags & B_MEDIA_SEEK_CLOSEST_FORWARD) != 0)
482 		searchFlags = 0;
483 
484 	if (fSeekByBytes) {
485 		searchFlags |= AVSEEK_FLAG_BYTE;
486 
487 		BAutolock _(fSourceLock);
488 		int64_t fileSize;
489 
490 		if (fSource->GetSize(&fileSize) != B_OK)
491 			return B_NOT_SUPPORTED;
492 
493 		int64_t duration = Duration();
494 		if (duration == 0)
495 			return B_NOT_SUPPORTED;
496 
497 		timeStamp = int64_t(fileSize * ((double)timeStamp / duration));
498 		if ((flags & B_MEDIA_SEEK_CLOSEST_BACKWARD) != 0) {
499 			timeStamp -= 65536;
500 			if (timeStamp < 0)
501 				timeStamp = 0;
502 		}
503 
504 		bool seekAgain = true;
505 		bool seekForward = true;
506 		bigtime_t lastFoundTime = -1;
507 		int64_t closestTimeStampBackwards = -1;
508 		while (seekAgain) {
509 			if (avformat_seek_file(fContext, -1, INT64_MIN, timeStamp,
510 				INT64_MAX, searchFlags) < 0) {
511 				TRACE("  avformat_seek_file() (by bytes) failed.\n");
512 				return B_ERROR;
513 			}
514 			seekAgain = false;
515 
516 			// Our last packet is toast in any case. Read the next one so we
517 			// know where we really seeked.
518 			fReusePacket = false;
519 			if (_NextPacket(true) == B_OK) {
520 				while (fPacket.pts == AV_NOPTS_VALUE) {
521 					fReusePacket = false;
522 					if (_NextPacket(true) != B_OK)
523 						return B_ERROR;
524 				}
525 				if (fPacket.pos >= 0)
526 					timeStamp = fPacket.pos;
527 				bigtime_t foundTime
528 					= _ConvertFromStreamTimeBase(fPacket.pts);
529 				if (foundTime != lastFoundTime) {
530 					lastFoundTime = foundTime;
531 					if (foundTime > *time) {
532 						if (closestTimeStampBackwards >= 0) {
533 							timeStamp = closestTimeStampBackwards;
534 							seekAgain = true;
535 							seekForward = false;
536 							continue;
537 						}
538 						int64_t diff = int64_t(fileSize
539 							* ((double)(foundTime - *time) / (2 * duration)));
540 						if (diff < 8192)
541 							break;
542 						timeStamp -= diff;
543 						TRACE_SEEK("  need to seek back (%lld) (time: %.2f "
544 							"-> %.2f)\n", timeStamp, *time / 1000000.0,
545 							foundTime / 1000000.0);
546 						if (timeStamp < 0)
547 							foundTime = 0;
548 						else {
549 							seekAgain = true;
550 							continue;
551 						}
552 					} else if (seekForward && foundTime < *time - 100000) {
553 						closestTimeStampBackwards = timeStamp;
554 						int64_t diff = int64_t(fileSize
555 							* ((double)(*time - foundTime) / (2 * duration)));
556 						if (diff < 8192)
557 							break;
558 						timeStamp += diff;
559 						TRACE_SEEK("  need to seek forward (%lld) (time: "
560 							"%.2f -> %.2f)\n", timeStamp, *time / 1000000.0,
561 							foundTime / 1000000.0);
562 						if (timeStamp > duration)
563 							foundTime = duration;
564 						else {
565 							seekAgain = true;
566 							continue;
567 						}
568 					}
569 				}
570 				TRACE_SEEK("  found time: %lld -> %lld (%.2f)\n", *time,
571 					foundTime, foundTime / 1000000.0);
572 				*time = foundTime;
573 				*frame = (uint64)(*time * frameRate / 1000000LL + 0.5);
574 				TRACE_SEEK("  seeked frame: %lld\n", *frame);
575 			} else {
576 				TRACE_SEEK("  _NextPacket() failed!\n");
577 				return B_ERROR;
578 			}
579 		}
580 	} else {
581 		// We may not get a PTS from the next packet after seeking, so
582 		// we try to get an expected time from the index.
583 		int64_t streamTimeStamp = _ConvertToStreamTimeBase(*time);
584 		int index = av_index_search_timestamp(fStream, streamTimeStamp,
585 			searchFlags);
586 		if (index < 0) {
587 			TRACE("  av_index_search_timestamp() failed\n");
588 		} else {
589 			if (index > 0) {
590 				const AVIndexEntry* entry = avformat_index_get_entry(fStream, index);
591 				streamTimeStamp = entry->timestamp;
592 			} else {
593 				// Some demuxers use the first index entry to store some
594 				// other information, like the total playing time for example.
595 				// Assume the timeStamp of the first entry is alays 0.
596 				// TODO: Handle start-time offset?
597 				streamTimeStamp = 0;
598 			}
599 			bigtime_t foundTime = _ConvertFromStreamTimeBase(streamTimeStamp);
600 			bigtime_t timeDiff = foundTime > *time
601 				? foundTime - *time : *time - foundTime;
602 
603 			if (timeDiff > 1000000
604 				&& (fStreamBuildsIndexWhileReading
605 					|| index == avformat_index_get_entries_count(fStream) - 1)) {
606 				// If the stream is building the index on the fly while parsing
607 				// it, we only have entries in the index for positions already
608 				// decoded, i.e. we cannot seek into the future. In that case,
609 				// just assume that we can seek where we want and leave
610 				// time/frame unmodified. Since successfully seeking one time
611 				// will generate index entries for the seeked to position, we
612 				// need to remember this in fStreamBuildsIndexWhileReading,
613 				// since when seeking back there will be later index entries,
614 				// but we still want to ignore the found entry.
615 				fStreamBuildsIndexWhileReading = true;
616 				TRACE_SEEK("  Not trusting generic index entry. "
617 					"(Current count: %d)\n", fStream->nb_index_entries);
618 			} else {
619 				// If we found a reasonably time, write it into *time.
620 				// After seeking, we will try to read the sought time from
621 				// the next packet. If the packet has no PTS value, we may
622 				// still have a more accurate time from the index lookup.
623 				*time = foundTime;
624 			}
625 		}
626 
627 		if (avformat_seek_file(fContext, -1, INT64_MIN, timeStamp, INT64_MAX,
628 				searchFlags) < 0) {
629 			TRACE("  avformat_seek_file() failed.\n");
630 			// Try to fall back to av_seek_frame()
631 			timeStamp = _ConvertToStreamTimeBase(timeStamp);
632 			if (av_seek_frame(fContext, fStream->index, timeStamp,
633 				searchFlags) < 0) {
634 				TRACE("  avformat_seek_frame() failed as well.\n");
635 				// Fall back to seeking to the beginning by bytes
636 				timeStamp = 0;
637 				if (av_seek_frame(fContext, fStream->index, timeStamp,
638 						AVSEEK_FLAG_BYTE) < 0) {
639 					TRACE("  avformat_seek_frame() by bytes failed as "
640 						"well.\n");
641 					// Do not propagate error in any case. We fail if we can't
642 					// read another packet.
643 				} else
644 					*time = 0;
645 			}
646 		}
647 
648 		// Our last packet is toast in any case. Read the next one so
649 		// we know where we really sought.
650 		bigtime_t foundTime = *time;
651 
652 		fReusePacket = false;
653 		if (_NextPacket(true) == B_OK) {
654 			if (fPacket.pts != AV_NOPTS_VALUE)
655 				foundTime = _ConvertFromStreamTimeBase(fPacket.pts);
656 			else
657 				TRACE_SEEK("  no PTS in packet after seeking\n");
658 		} else
659 			TRACE_SEEK("  _NextPacket() failed!\n");
660 
661 		*time = foundTime;
662 		TRACE_SEEK("  sought time: %.2fs\n", *time / 1000000.0);
663 		*frame = (uint64)(*time * frameRate / 1000000.0 + 0.5);
664 		TRACE_SEEK("  sought frame: %lld\n", *frame);
665 	}
666 
667 	return B_OK;
668 }
669 
670 
671 status_t
672 StreamBase::GetNextChunk(const void** chunkBuffer,
673 	size_t* chunkSize, media_header* mediaHeader)
674 {
675 	BAutolock _(fStreamLock);
676 
677 	TRACE_PACKET("StreamBase::GetNextChunk()\n");
678 
679 	status_t ret = _NextPacket(false);
680 	if (ret != B_OK) {
681 		*chunkBuffer = NULL;
682 		*chunkSize = 0;
683 		return ret;
684 	}
685 
686 	// According to libavformat documentation, fPacket is valid until the
687 	// next call to av_read_frame(). This is what we want and we can share
688 	// the memory with the least overhead.
689 	*chunkBuffer = fPacket.data;
690 	*chunkSize = fPacket.size;
691 
692 	if (mediaHeader != NULL) {
693 		mediaHeader->type = fFormat.type;
694 		mediaHeader->buffer = 0;
695 		mediaHeader->destination = -1;
696 		mediaHeader->time_source = -1;
697 		mediaHeader->size_used = fPacket.size;
698 
699 		// Use the presentation timestamp if available (that is not always the case)
700 		// Use the decoding timestamp as a fallback, that is guaranteed to be set by av_read_frame
701 		bigtime_t presentationTimeStamp;
702 		if (fPacket.pts != AV_NOPTS_VALUE)
703 			presentationTimeStamp = fPacket.pts;
704 		else
705 			presentationTimeStamp = fPacket.dts;
706 
707 		mediaHeader->start_time	= _ConvertFromStreamTimeBase(presentationTimeStamp);
708 		mediaHeader->file_pos = fPacket.pos;
709 		mediaHeader->data_offset = 0;
710 		switch (mediaHeader->type) {
711 			case B_MEDIA_RAW_AUDIO:
712 				break;
713 			case B_MEDIA_ENCODED_AUDIO:
714 				mediaHeader->u.encoded_audio.buffer_flags
715 					= (fPacket.flags & AV_PKT_FLAG_KEY) ? B_MEDIA_KEY_FRAME : 0;
716 				break;
717 			case B_MEDIA_RAW_VIDEO:
718 				mediaHeader->u.raw_video.line_count
719 					= fFormat.u.raw_video.display.line_count;
720 				break;
721 			case B_MEDIA_ENCODED_VIDEO:
722 				mediaHeader->u.encoded_video.field_flags
723 					= (fPacket.flags & AV_PKT_FLAG_KEY) ? B_MEDIA_KEY_FRAME : 0;
724 				mediaHeader->u.encoded_video.line_count
725 					= fFormat.u.encoded_video.output.display.line_count;
726 				break;
727 			default:
728 				break;
729 		}
730 	}
731 
732 //	static bigtime_t pts[2];
733 //	static bigtime_t lastPrintTime = system_time();
734 //	static BLocker printLock;
735 //	if (fStream->index < 2) {
736 //		if (fPacket.pts != AV_NOPTS_VALUE)
737 //			pts[fStream->index] = _ConvertFromStreamTimeBase(fPacket.pts);
738 //		printLock.Lock();
739 //		bigtime_t now = system_time();
740 //		if (now - lastPrintTime > 1000000) {
741 //			printf("PTS: %.4f/%.4f, diff: %.4f\r", pts[0] / 1000000.0,
742 //				pts[1] / 1000000.0, (pts[0] - pts[1]) / 1000000.0);
743 //			fflush(stdout);
744 //			lastPrintTime = now;
745 //		}
746 //		printLock.Unlock();
747 //	}
748 
749 	return B_OK;
750 }
751 
752 
753 // #pragma mark -
754 
755 
756 /*static*/ int
757 StreamBase::_Read(void* cookie, uint8* buffer, int bufferSize)
758 {
759 	StreamBase* stream = reinterpret_cast<StreamBase*>(cookie);
760 
761 	BAutolock _(stream->fSourceLock);
762 
763 	TRACE_IO("StreamBase::_Read(%p, %p, %d) position: %lld\n",
764 		cookie, buffer, bufferSize, stream->fPosition);
765 
766 	if (stream->fPosition != stream->fSource->Position()) {
767 		TRACE_IO("StreamBase::_Read fSource position: %lld\n",
768 			stream->fSource->Position());
769 
770 		off_t position
771 			= stream->fSource->Seek(stream->fPosition, SEEK_SET);
772 		if (position != stream->fPosition)
773 			return -1;
774 	}
775 
776 	ssize_t read = stream->fSource->Read(buffer, bufferSize);
777 	if (read > 0)
778 		stream->fPosition += read;
779 
780 	TRACE_IO("  read: %ld\n", read);
781 	return (int)read;
782 
783 }
784 
785 
786 /*static*/ off_t
787 StreamBase::_Seek(void* cookie, off_t offset, int whence)
788 {
789 	TRACE_IO("StreamBase::_Seek(%p, %lld, %d)\n",
790 		cookie, offset, whence);
791 
792 	StreamBase* stream = reinterpret_cast<StreamBase*>(cookie);
793 
794 	BAutolock _(stream->fSourceLock);
795 
796 	// Support for special file size retrieval API without seeking
797 	// anywhere:
798 	if (whence == AVSEEK_SIZE) {
799 		off_t size;
800 		if (stream->fSource->GetSize(&size) == B_OK)
801 			return size;
802 		return -1;
803 	}
804 
805 	// If not requested to seek to an absolute position, we need to
806 	// confirm that the stream is currently at the position that we
807 	// think it is.
808 	if (whence != SEEK_SET
809 		&& stream->fPosition != stream->fSource->Position()) {
810 		off_t position
811 			= stream->fSource->Seek(stream->fPosition, SEEK_SET);
812 		if (position != stream->fPosition)
813 			return -1;
814 	}
815 
816 	off_t position = stream->fSource->Seek(offset, whence);
817 	TRACE_IO("  position: %lld\n", position);
818 	if (position < 0)
819 		return -1;
820 
821 	stream->fPosition = position;
822 
823 	return position;
824 }
825 
826 
827 status_t
828 StreamBase::_NextPacket(bool reuse)
829 {
830 	TRACE_PACKET("StreamBase::_NextPacket(%d)\n", reuse);
831 
832 	if (fReusePacket) {
833 		// The last packet was marked for reuse, so we keep using it.
834 		TRACE_PACKET("  re-using last packet\n");
835 		fReusePacket = reuse;
836 		return B_OK;
837 	}
838 
839 	av_packet_unref(&fPacket);
840 
841 	while (true) {
842 		if (av_read_frame(fContext, &fPacket) < 0) {
843 			// NOTE: Even though we may get the error for a different stream,
844 			// av_read_frame() is not going to be successful from here on, so
845 			// it doesn't matter
846 			fReusePacket = false;
847 			return B_LAST_BUFFER_ERROR;
848 		}
849 
850 		if (fPacket.stream_index == Index())
851 			break;
852 
853 		// This is a packet from another stream, ignore it.
854 		av_packet_unref(&fPacket);
855 	}
856 
857 	// Mark this packet with the new reuse flag.
858 	fReusePacket = reuse;
859 	return B_OK;
860 }
861 
862 
863 int64_t
864 StreamBase::_ConvertToStreamTimeBase(bigtime_t time) const
865 {
866 	int64 timeStamp = int64_t((double)time * fStream->time_base.den
867 		/ (1000000.0 * fStream->time_base.num) + 0.5);
868 	if (fStream->start_time != AV_NOPTS_VALUE)
869 		timeStamp += fStream->start_time;
870 	return timeStamp;
871 }
872 
873 
874 bigtime_t
875 StreamBase::_ConvertFromStreamTimeBase(int64_t time) const
876 {
877 	if (fStream->start_time != AV_NOPTS_VALUE)
878 		time -= fStream->start_time;
879 
880 	return bigtime_t(1000000.0 * time * fStream->time_base.num
881 		/ fStream->time_base.den + 0.5);
882 }
883 
884 
885 // #pragma mark - AVFormatReader::Stream
886 
887 
888 class AVFormatReader::Stream : public StreamBase {
889 public:
890 								Stream(BMediaIO* source,
891 									BLocker* streamLock);
892 	virtual						~Stream();
893 
894 	// Setup this stream to point to the AVStream at the given streamIndex.
895 	// This will also initialize the media_format.
896 	virtual	status_t			Init(int32 streamIndex);
897 
898 			status_t			GetMetaData(BMessage* data);
899 
900 	// Support for AVFormatReader
901 			status_t			GetStreamInfo(int64* frameCount,
902 									bigtime_t* duration, media_format* format,
903 									const void** infoBuffer,
904 									size_t* infoSize) const;
905 
906 			status_t			FindKeyFrame(uint32 flags, int64* frame,
907 									bigtime_t* time) const;
908 	virtual	status_t			Seek(uint32 flags, int64* frame,
909 									bigtime_t* time);
910 
911 private:
912 	mutable	BLocker				fLock;
913 
914 			struct KeyframeInfo {
915 				bigtime_t		requestedTime;
916 				int64			requestedFrame;
917 				bigtime_t		reportedTime;
918 				int64			reportedFrame;
919 				uint32			seekFlags;
920 			};
921 	mutable	KeyframeInfo		fLastReportedKeyframe;
922 	mutable	StreamBase*			fGhostStream;
923 };
924 
925 
926 
927 AVFormatReader::Stream::Stream(BMediaIO* source, BLocker* streamLock)
928 	:
929 	StreamBase(source, streamLock, &fLock),
930 	fLock("stream lock"),
931 	fGhostStream(NULL)
932 {
933 	fLastReportedKeyframe.requestedTime = 0;
934 	fLastReportedKeyframe.requestedFrame = 0;
935 	fLastReportedKeyframe.reportedTime = 0;
936 	fLastReportedKeyframe.reportedFrame = 0;
937 }
938 
939 
940 AVFormatReader::Stream::~Stream()
941 {
942 	delete fGhostStream;
943 }
944 
945 
946 status_t
947 AVFormatReader::Stream::Init(int32 virtualIndex)
948 {
949 	TRACE("AVFormatReader::Stream::Init(%ld)\n", virtualIndex);
950 
951 	status_t ret = StreamBase::Init(virtualIndex);
952 	if (ret != B_OK)
953 		return ret;
954 
955 	// Get a pointer to the AVCodecPaarameters for the stream at streamIndex.
956 	AVCodecParameters* codecParams = fStream->codecpar;
957 
958 	// initialize the media_format for this stream
959 	media_format* format = &fFormat;
960 	format->Clear();
961 
962 	media_format_description description;
963 
964 	// Set format family and type depending on codec_type of the stream.
965 	switch (codecParams->codec_type) {
966 		case AVMEDIA_TYPE_AUDIO:
967 			if ((codecParams->codec_id >= AV_CODEC_ID_PCM_S16LE)
968 				&& (codecParams->codec_id <= AV_CODEC_ID_PCM_U8)) {
969 				TRACE("  raw audio\n");
970 				format->type = B_MEDIA_RAW_AUDIO;
971 				description.family = B_ANY_FORMAT_FAMILY;
972 				// This will then apparently be handled by the (built into
973 				// BMediaTrack) RawDecoder.
974 			} else {
975 				TRACE("  encoded audio\n");
976 				format->type = B_MEDIA_ENCODED_AUDIO;
977 				description.family = B_MISC_FORMAT_FAMILY;
978 				description.u.misc.file_format = 'ffmp';
979 			}
980 			break;
981 		case AVMEDIA_TYPE_VIDEO:
982 			TRACE("  encoded video\n");
983 			format->type = B_MEDIA_ENCODED_VIDEO;
984 			description.family = B_MISC_FORMAT_FAMILY;
985 			description.u.misc.file_format = 'ffmp';
986 			break;
987 		default:
988 			TRACE("  unknown type\n");
989 			format->type = B_MEDIA_UNKNOWN_TYPE;
990 			return B_ERROR;
991 			break;
992 	}
993 
994 	if (format->type == B_MEDIA_RAW_AUDIO) {
995 		// We cannot describe all raw-audio formats, some are unsupported.
996 		switch (codecParams->codec_id) {
997 			case AV_CODEC_ID_PCM_S16LE:
998 				format->u.raw_audio.format
999 					= media_raw_audio_format::B_AUDIO_SHORT;
1000 				format->u.raw_audio.byte_order
1001 					= B_MEDIA_LITTLE_ENDIAN;
1002 				break;
1003 			case AV_CODEC_ID_PCM_S16BE:
1004 				format->u.raw_audio.format
1005 					= media_raw_audio_format::B_AUDIO_SHORT;
1006 				format->u.raw_audio.byte_order
1007 					= B_MEDIA_BIG_ENDIAN;
1008 				break;
1009 			case AV_CODEC_ID_PCM_U16LE:
1010 //				format->u.raw_audio.format
1011 //					= media_raw_audio_format::B_AUDIO_USHORT;
1012 //				format->u.raw_audio.byte_order
1013 //					= B_MEDIA_LITTLE_ENDIAN;
1014 				return B_NOT_SUPPORTED;
1015 				break;
1016 			case AV_CODEC_ID_PCM_U16BE:
1017 //				format->u.raw_audio.format
1018 //					= media_raw_audio_format::B_AUDIO_USHORT;
1019 //				format->u.raw_audio.byte_order
1020 //					= B_MEDIA_BIG_ENDIAN;
1021 				return B_NOT_SUPPORTED;
1022 				break;
1023 			case AV_CODEC_ID_PCM_S8:
1024 				format->u.raw_audio.format
1025 					= media_raw_audio_format::B_AUDIO_CHAR;
1026 				break;
1027 			case AV_CODEC_ID_PCM_U8:
1028 				format->u.raw_audio.format
1029 					= media_raw_audio_format::B_AUDIO_UCHAR;
1030 				break;
1031 			default:
1032 				return B_NOT_SUPPORTED;
1033 				break;
1034 		}
1035 	} else {
1036 		if (description.family == B_MISC_FORMAT_FAMILY)
1037 			description.u.misc.codec = codecParams->codec_id;
1038 
1039 		BMediaFormats formats;
1040 		status_t status = formats.GetFormatFor(description, format);
1041 		if (status < B_OK)
1042 			TRACE("  formats.GetFormatFor() error: %s\n", strerror(status));
1043 
1044 		format->user_data_type = B_CODEC_TYPE_INFO;
1045 		*(uint32*)format->user_data = codecParams->codec_tag;
1046 		format->user_data[4] = 0;
1047 	}
1048 
1049 	format->require_flags = 0;
1050 	format->deny_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
1051 
1052 	switch (format->type) {
1053 		case B_MEDIA_RAW_AUDIO:
1054 			format->u.raw_audio.frame_rate = (float)codecParams->sample_rate;
1055 			format->u.raw_audio.channel_count = codecParams->channels;
1056 			format->u.raw_audio.channel_mask = codecParams->channel_layout;
1057 			ConvertAVSampleFormatToRawAudioFormat(
1058 				(AVSampleFormat)codecParams->format,
1059 				format->u.raw_audio.format);
1060 			format->u.raw_audio.buffer_size = 0;
1061 
1062 			// Read one packet and mark it for later re-use. (So our first
1063 			// GetNextChunk() call does not read another packet.)
1064 			if (_NextPacket(true) == B_OK) {
1065 				TRACE("  successfully determined audio buffer size: %d\n",
1066 					fPacket.size);
1067 				format->u.raw_audio.buffer_size = fPacket.size;
1068 			}
1069 			break;
1070 
1071 		case B_MEDIA_ENCODED_AUDIO:
1072 			format->u.encoded_audio.bit_rate = codecParams->bit_rate;
1073 			format->u.encoded_audio.frame_size = codecParams->frame_size;
1074 			// Fill in some info about possible output format
1075 			format->u.encoded_audio.output
1076 				= media_multi_audio_format::wildcard;
1077 			format->u.encoded_audio.output.frame_rate
1078 				= (float)codecParams->sample_rate;
1079 			// Channel layout bits match in Be API and FFmpeg.
1080 			format->u.encoded_audio.output.channel_count
1081 				= codecParams->channels;
1082 			format->u.encoded_audio.multi_info.channel_mask
1083 				= codecParams->channel_layout;
1084 			format->u.encoded_audio.output.byte_order
1085 				= avformat_to_beos_byte_order(
1086 					(AVSampleFormat)codecParams->format);
1087 
1088 			ConvertAVSampleFormatToRawAudioFormat(
1089 					(AVSampleFormat)codecParams->format,
1090 				format->u.encoded_audio.output.format);
1091 
1092 			if (codecParams->block_align > 0) {
1093 				format->u.encoded_audio.output.buffer_size
1094 					= codecParams->block_align;
1095 			} else {
1096 				format->u.encoded_audio.output.buffer_size
1097 					= codecParams->frame_size * codecParams->channels
1098 						* (format->u.encoded_audio.output.format
1099 							& media_raw_audio_format::B_AUDIO_SIZE_MASK);
1100 			}
1101 			break;
1102 
1103 		case B_MEDIA_ENCODED_VIDEO:
1104 // TODO: Specifying any of these seems to throw off the format matching
1105 // later on.
1106 //			format->u.encoded_video.avg_bit_rate = codecParams->bit_rate;
1107 //			format->u.encoded_video.max_bit_rate = codecParams->bit_rate
1108 //				+ codecParams->bit_rate_tolerance;
1109 
1110 //			format->u.encoded_video.encoding
1111 //				= media_encoded_video_format::B_ANY;
1112 
1113 //			format->u.encoded_video.frame_size = 1;
1114 //			format->u.encoded_video.forward_history = 0;
1115 //			format->u.encoded_video.backward_history = 0;
1116 
1117 			format->u.encoded_video.output.field_rate = FrameRate();
1118 			format->u.encoded_video.output.interlace = 1;
1119 
1120 			format->u.encoded_video.output.first_active = 0;
1121 			format->u.encoded_video.output.last_active
1122 				= codecParams->height - 1;
1123 				// TODO: Maybe libavformat actually provides that info
1124 				// somewhere...
1125 			format->u.encoded_video.output.orientation
1126 				= B_VIDEO_TOP_LEFT_RIGHT;
1127 
1128 			ConvertAVCodecParametersToVideoAspectWidthAndHeight(*codecParams,
1129 				format->u.encoded_video.output.pixel_width_aspect,
1130 				format->u.encoded_video.output.pixel_height_aspect);
1131 
1132 			format->u.encoded_video.output.display.format
1133 				= pixfmt_to_colorspace(codecParams->format);
1134 			format->u.encoded_video.output.display.line_width
1135 				= codecParams->width;
1136 			format->u.encoded_video.output.display.line_count
1137 				= codecParams->height;
1138 			TRACE("  width/height: %d/%d\n", codecParams->width,
1139 				codecParams->height);
1140 			format->u.encoded_video.output.display.bytes_per_row = 0;
1141 			format->u.encoded_video.output.display.pixel_offset = 0;
1142 			format->u.encoded_video.output.display.line_offset = 0;
1143 			format->u.encoded_video.output.display.flags = 0; // TODO
1144 
1145 			break;
1146 
1147 		default:
1148 			// This is an unknown format to us.
1149 			break;
1150 	}
1151 
1152 	// Add the meta data, if any
1153 	if (codecParams->extradata_size > 0) {
1154 		format->SetMetaData(codecParams->extradata,
1155 			codecParams->extradata_size);
1156 		TRACE("  extradata: %p\n", format->MetaData());
1157 	}
1158 
1159 	TRACE("  extradata_size: %d\n", codecParams->extradata_size);
1160 //	TRACE("  intra_matrix: %p\n", codecParams->intra_matrix);
1161 //	TRACE("  inter_matrix: %p\n", codecParams->inter_matrix);
1162 //	TRACE("  get_buffer(): %p\n", codecParams->get_buffer);
1163 //	TRACE("  release_buffer(): %p\n", codecParams->release_buffer);
1164 
1165 #ifdef TRACE_AVFORMAT_READER
1166 	char formatString[512];
1167 	if (string_for_format(*format, formatString, sizeof(formatString)))
1168 		TRACE("  format: %s\n", formatString);
1169 
1170 	uint32 encoding = format->Encoding();
1171 	TRACE("  encoding '%.4s'\n", (char*)&encoding);
1172 #endif
1173 
1174 	return B_OK;
1175 }
1176 
1177 
1178 status_t
1179 AVFormatReader::Stream::GetMetaData(BMessage* data)
1180 {
1181 	BAutolock _(&fLock);
1182 
1183 	avdictionary_to_message(fStream->metadata, data);
1184 
1185 	return B_OK;
1186 }
1187 
1188 
1189 status_t
1190 AVFormatReader::Stream::GetStreamInfo(int64* frameCount,
1191 	bigtime_t* duration, media_format* format, const void** infoBuffer,
1192 	size_t* infoSize) const
1193 {
1194 	BAutolock _(&fLock);
1195 
1196 	TRACE("AVFormatReader::Stream::GetStreamInfo(%ld)\n",
1197 		VirtualIndex());
1198 
1199 	double frameRate = FrameRate();
1200 	TRACE("  frameRate: %.4f\n", frameRate);
1201 
1202 	#ifdef TRACE_AVFORMAT_READER
1203 	if (fStream->start_time != AV_NOPTS_VALUE) {
1204 		bigtime_t startTime = _ConvertFromStreamTimeBase(fStream->start_time);
1205 		TRACE("  start_time: %lld or %.5fs\n", startTime,
1206 			startTime / 1000000.0);
1207 		// TODO: Handle start time in FindKeyFrame() and Seek()?!
1208 	}
1209 	#endif // TRACE_AVFORMAT_READER
1210 
1211 	*duration = Duration();
1212 
1213 	TRACE("  duration: %lld or %.5fs\n", *duration, *duration / 1000000.0);
1214 
1215 	#if 0
1216 	if (fStream->nb_index_entries > 0) {
1217 		TRACE("  dump of index entries:\n");
1218 		int count = 5;
1219 		int firstEntriesCount = min_c(fStream->nb_index_entries, count);
1220 		int i = 0;
1221 		for (; i < firstEntriesCount; i++) {
1222 			AVIndexEntry& entry = fStream->index_entries[i];
1223 			bigtime_t timeGlobal = entry.timestamp;
1224 			bigtime_t timeNative = _ConvertFromStreamTimeBase(timeGlobal);
1225 			TRACE("    [%d] native: %.5fs global: %.5fs\n", i,
1226 				timeNative / 1000000.0f, timeGlobal / 1000000.0f);
1227 		}
1228 		if (fStream->nb_index_entries - count > i) {
1229 			i = fStream->nb_index_entries - count;
1230 			TRACE("    ...\n");
1231 			for (; i < fStream->nb_index_entries; i++) {
1232 				AVIndexEntry& entry = fStream->index_entries[i];
1233 				bigtime_t timeGlobal = entry.timestamp;
1234 				bigtime_t timeNative = _ConvertFromStreamTimeBase(timeGlobal);
1235 				TRACE("    [%d] native: %.5fs global: %.5fs\n", i,
1236 					timeNative / 1000000.0f, timeGlobal / 1000000.0f);
1237 			}
1238 		}
1239 	}
1240 	#endif
1241 
1242 	*frameCount = fStream->nb_frames * fStream->codecpar->frame_size;
1243 	if (*frameCount == 0) {
1244 		// Calculate from duration and frame rate
1245 		*frameCount = (int64)(*duration * frameRate / 1000000LL);
1246 		TRACE("  frameCount calculated: %lld, from context: %lld\n",
1247 			*frameCount, fStream->nb_frames);
1248 	} else
1249 		TRACE("  frameCount: %lld\n", *frameCount);
1250 
1251 	*format = fFormat;
1252 
1253 	*infoBuffer = fStream->codecpar->extradata;
1254 	*infoSize = fStream->codecpar->extradata_size;
1255 
1256 	return B_OK;
1257 }
1258 
1259 
1260 status_t
1261 AVFormatReader::Stream::FindKeyFrame(uint32 flags, int64* frame,
1262 	bigtime_t* time) const
1263 {
1264 	BAutolock _(&fLock);
1265 
1266 	if (fContext == NULL || fStream == NULL)
1267 		return B_NO_INIT;
1268 
1269 	TRACE_FIND("AVFormatReader::Stream::FindKeyFrame(%ld,%s%s%s%s, "
1270 		"%lld, %lld)\n", VirtualIndex(),
1271 		(flags & B_MEDIA_SEEK_TO_FRAME) ? " B_MEDIA_SEEK_TO_FRAME" : "",
1272 		(flags & B_MEDIA_SEEK_TO_TIME) ? " B_MEDIA_SEEK_TO_TIME" : "",
1273 		(flags & B_MEDIA_SEEK_CLOSEST_BACKWARD)
1274 			? " B_MEDIA_SEEK_CLOSEST_BACKWARD" : "",
1275 		(flags & B_MEDIA_SEEK_CLOSEST_FORWARD)
1276 			? " B_MEDIA_SEEK_CLOSEST_FORWARD" : "",
1277 		*frame, *time);
1278 
1279 	bool inLastRequestedRange = false;
1280 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1281 		if (fLastReportedKeyframe.reportedFrame
1282 			<= fLastReportedKeyframe.requestedFrame) {
1283 			inLastRequestedRange
1284 				= *frame >= fLastReportedKeyframe.reportedFrame
1285 					&& *frame <= fLastReportedKeyframe.requestedFrame;
1286 		} else {
1287 			inLastRequestedRange
1288 				= *frame >= fLastReportedKeyframe.requestedFrame
1289 					&& *frame <= fLastReportedKeyframe.reportedFrame;
1290 		}
1291 	} else if ((flags & B_MEDIA_SEEK_TO_FRAME) == 0) {
1292 		if (fLastReportedKeyframe.reportedTime
1293 			<= fLastReportedKeyframe.requestedTime) {
1294 			inLastRequestedRange
1295 				= *time >= fLastReportedKeyframe.reportedTime
1296 					&& *time <= fLastReportedKeyframe.requestedTime;
1297 		} else {
1298 			inLastRequestedRange
1299 				= *time >= fLastReportedKeyframe.requestedTime
1300 					&& *time <= fLastReportedKeyframe.reportedTime;
1301 		}
1302 	}
1303 
1304 	if (inLastRequestedRange) {
1305 		*frame = fLastReportedKeyframe.reportedFrame;
1306 		*time = fLastReportedKeyframe.reportedTime;
1307 		TRACE_FIND("  same as last reported keyframe\n");
1308 		return B_OK;
1309 	}
1310 
1311 	double frameRate = FrameRate();
1312 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0)
1313 		*time = (bigtime_t)(*frame * 1000000.0 / frameRate + 0.5);
1314 
1315 	status_t ret;
1316 	if (fGhostStream == NULL) {
1317 		BAutolock _(fSourceLock);
1318 
1319 		fGhostStream = new(std::nothrow) StreamBase(fSource, fSourceLock,
1320 			&fLock);
1321 		if (fGhostStream == NULL) {
1322 			TRACE("  failed to allocate ghost stream\n");
1323 			return B_NO_MEMORY;
1324 		}
1325 
1326 		ret = fGhostStream->Open();
1327 		if (ret != B_OK) {
1328 			TRACE("  ghost stream failed to open: %s\n", strerror(ret));
1329 			return B_ERROR;
1330 		}
1331 
1332 		ret = fGhostStream->Init(fVirtualIndex);
1333 		if (ret != B_OK) {
1334 			TRACE("  ghost stream failed to init: %s\n", strerror(ret));
1335 			return B_ERROR;
1336 		}
1337 	}
1338 	fLastReportedKeyframe.requestedFrame = *frame;
1339 	fLastReportedKeyframe.requestedTime = *time;
1340 	fLastReportedKeyframe.seekFlags = flags;
1341 
1342 	ret = fGhostStream->Seek(flags, frame, time);
1343 	if (ret != B_OK) {
1344 		TRACE("  ghost stream failed to seek: %s\n", strerror(ret));
1345 		return B_ERROR;
1346 	}
1347 
1348 	fLastReportedKeyframe.reportedFrame = *frame;
1349 	fLastReportedKeyframe.reportedTime = *time;
1350 
1351 	TRACE_FIND("  found time: %.2fs\n", *time / 1000000.0);
1352 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1353 		*frame = int64_t(*time * FrameRate() / 1000000.0 + 0.5);
1354 		TRACE_FIND("  found frame: %lld\n", *frame);
1355 	}
1356 
1357 	return B_OK;
1358 }
1359 
1360 
1361 status_t
1362 AVFormatReader::Stream::Seek(uint32 flags, int64* frame, bigtime_t* time)
1363 {
1364 	BAutolock _(&fLock);
1365 
1366 	if (fContext == NULL || fStream == NULL)
1367 		return B_NO_INIT;
1368 
1369 	// Put the old requested values into frame/time, since we already know
1370 	// that the sought frame/time will then match the reported values.
1371 	// TODO: Will not work if client changes seek flags (from backwards to
1372 	// forward or vice versa)!!
1373 	bool inLastRequestedRange = false;
1374 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1375 		if (fLastReportedKeyframe.reportedFrame
1376 			<= fLastReportedKeyframe.requestedFrame) {
1377 			inLastRequestedRange
1378 				= *frame >= fLastReportedKeyframe.reportedFrame
1379 					&& *frame <= fLastReportedKeyframe.requestedFrame;
1380 		} else {
1381 			inLastRequestedRange
1382 				= *frame >= fLastReportedKeyframe.requestedFrame
1383 					&& *frame <= fLastReportedKeyframe.reportedFrame;
1384 		}
1385 	} else if ((flags & B_MEDIA_SEEK_TO_FRAME) == 0) {
1386 		if (fLastReportedKeyframe.reportedTime
1387 			<= fLastReportedKeyframe.requestedTime) {
1388 			inLastRequestedRange
1389 				= *time >= fLastReportedKeyframe.reportedTime
1390 					&& *time <= fLastReportedKeyframe.requestedTime;
1391 		} else {
1392 			inLastRequestedRange
1393 				= *time >= fLastReportedKeyframe.requestedTime
1394 					&& *time <= fLastReportedKeyframe.reportedTime;
1395 		}
1396 	}
1397 
1398 	if (inLastRequestedRange) {
1399 		*frame = fLastReportedKeyframe.requestedFrame;
1400 		*time = fLastReportedKeyframe.requestedTime;
1401 		flags = fLastReportedKeyframe.seekFlags;
1402 	}
1403 
1404 	return StreamBase::Seek(flags, frame, time);
1405 }
1406 
1407 
1408 // #pragma mark - AVFormatReader
1409 
1410 
1411 AVFormatReader::AVFormatReader()
1412 	:
1413 	fCopyright(""),
1414 	fStreams(NULL),
1415 	fSourceLock("source I/O lock")
1416 {
1417 	TRACE("AVFormatReader::AVFormatReader\n");
1418 }
1419 
1420 
1421 AVFormatReader::~AVFormatReader()
1422 {
1423 	TRACE("AVFormatReader::~AVFormatReader\n");
1424 	if (fStreams != NULL) {
1425 		// The client was supposed to call FreeCookie() on all
1426 		// allocated streams. Deleting the first stream is always
1427 		// prevented, we delete the other ones just in case.
1428 		int32 count = fStreams[0]->CountStreams();
1429 		for (int32 i = 0; i < count; i++)
1430 			delete fStreams[i];
1431 		delete[] fStreams;
1432 	}
1433 }
1434 
1435 
1436 // #pragma mark -
1437 
1438 
1439 const char*
1440 AVFormatReader::Copyright()
1441 {
1442 	if (fCopyright.Length() <= 0) {
1443 		BMessage message;
1444 		if (GetMetaData(&message) == B_OK)
1445 			message.FindString("copyright", &fCopyright);
1446 	}
1447 	return fCopyright.String();
1448 }
1449 
1450 
1451 status_t
1452 AVFormatReader::Sniff(int32* _streamCount)
1453 {
1454 	TRACE("AVFormatReader::Sniff\n");
1455 
1456 	BMediaIO* source = dynamic_cast<BMediaIO*>(Source());
1457 	if (source == NULL) {
1458 		TRACE("  not a BMediaIO, but we need it to be one.\n");
1459 		return B_NOT_SUPPORTED;
1460 	}
1461 
1462 	Stream* stream = new(std::nothrow) Stream(source,
1463 		&fSourceLock);
1464 	if (stream == NULL) {
1465 		ERROR("AVFormatReader::Sniff() - failed to allocate Stream\n");
1466 		return B_NO_MEMORY;
1467 	}
1468 
1469 	ObjectDeleter<Stream> streamDeleter(stream);
1470 
1471 	status_t ret = stream->Open();
1472 	if (ret != B_OK) {
1473 		TRACE("  failed to detect stream: %s\n", strerror(ret));
1474 		return ret;
1475 	}
1476 
1477 	delete[] fStreams;
1478 	fStreams = NULL;
1479 
1480 	int32 streamCount = stream->CountStreams();
1481 	if (streamCount == 0) {
1482 		TRACE("  failed to detect any streams: %s\n", strerror(ret));
1483 		return B_ERROR;
1484 	}
1485 
1486 	fStreams = new(std::nothrow) Stream*[streamCount];
1487 	if (fStreams == NULL) {
1488 		ERROR("AVFormatReader::Sniff() - failed to allocate streams\n");
1489 		return B_NO_MEMORY;
1490 	}
1491 
1492 	memset(fStreams, 0, sizeof(Stream*) * streamCount);
1493 	fStreams[0] = stream;
1494 	streamDeleter.Detach();
1495 
1496 	#ifdef TRACE_AVFORMAT_READER
1497 	av_dump_format(const_cast<AVFormatContext*>(stream->Context()), 0, "", 0);
1498 	#endif
1499 
1500 	if (_streamCount != NULL)
1501 		*_streamCount = streamCount;
1502 
1503 	return B_OK;
1504 }
1505 
1506 
1507 void
1508 AVFormatReader::GetFileFormatInfo(media_file_format* mff)
1509 {
1510 	TRACE("AVFormatReader::GetFileFormatInfo\n");
1511 
1512 	if (fStreams == NULL)
1513 		return;
1514 
1515 	// The first cookie is always there!
1516 	const AVFormatContext* context = fStreams[0]->Context();
1517 
1518 	if (context == NULL || context->iformat == NULL) {
1519 		TRACE("  no AVFormatContext or AVInputFormat!\n");
1520 		return;
1521 	}
1522 
1523 	const media_file_format* format = demuxer_format_for(context->iformat);
1524 
1525 	mff->capabilities = media_file_format::B_READABLE
1526 		| media_file_format::B_KNOWS_ENCODED_VIDEO
1527 		| media_file_format::B_KNOWS_ENCODED_AUDIO
1528 		| media_file_format::B_IMPERFECTLY_SEEKABLE;
1529 
1530 	if (format != NULL) {
1531 		mff->family = format->family;
1532 	} else {
1533 		TRACE("  no DemuxerFormat for AVInputFormat!\n");
1534 		mff->family = B_MISC_FORMAT_FAMILY;
1535 	}
1536 
1537 	mff->version = 100;
1538 
1539 	if (format != NULL) {
1540 		strlcpy(mff->mime_type, format->mime_type, sizeof(mff->mime_type));
1541 	} else {
1542 		// TODO: Would be nice to be able to provide this from AVInputFormat,
1543 		// maybe by extending the FFmpeg code itself (all demuxers).
1544 		mff->mime_type[0] = '\0';
1545 	}
1546 
1547 	if (context->iformat->extensions != NULL)
1548 		strlcpy(mff->file_extension, context->iformat->extensions, sizeof(mff->file_extension));
1549 	else {
1550 		TRACE("  no file extensions for AVInputFormat.\n");
1551 		mff->file_extension[0] = '\0';
1552 	}
1553 
1554 	if (context->iformat->name != NULL)
1555 		strlcpy(mff->short_name,  context->iformat->name, sizeof(mff->short_name));
1556 	else {
1557 		TRACE("  no short name for AVInputFormat.\n");
1558 		mff->short_name[0] = '\0';
1559 	}
1560 
1561 	if (context->iformat->long_name != NULL) {
1562 		snprintf(mff->pretty_name, sizeof(mff->pretty_name), "%s (FFmpeg)",
1563 			context->iformat->long_name);
1564 	} else if (format != NULL)
1565 		snprintf(mff->pretty_name, sizeof(mff->pretty_name), "%.54s (FFmpeg)", format->pretty_name);
1566 	else
1567 		strlcpy(mff->pretty_name, "Unknown (FFmpeg)", sizeof(mff->pretty_name));
1568 }
1569 
1570 
1571 status_t
1572 AVFormatReader::GetMetaData(BMessage* _data)
1573 {
1574 	// The first cookie is always there!
1575 	const AVFormatContext* context = fStreams[0]->Context();
1576 
1577 	if (context == NULL)
1578 		return B_NO_INIT;
1579 
1580 	avdictionary_to_message(context->metadata, _data);
1581 
1582 	// Add chapter info
1583 	for (unsigned i = 0; i < context->nb_chapters; i++) {
1584 		AVChapter* chapter = context->chapters[i];
1585 		BMessage chapterData;
1586 		chapterData.AddInt64("start", bigtime_t(1000000.0
1587 			* chapter->start * chapter->time_base.num
1588 			/ chapter->time_base.den + 0.5));
1589 		chapterData.AddInt64("end", bigtime_t(1000000.0
1590 			* chapter->end * chapter->time_base.num
1591 			/ chapter->time_base.den + 0.5));
1592 
1593 		avdictionary_to_message(chapter->metadata, &chapterData);
1594 		_data->AddMessage("be:chapter", &chapterData);
1595 	}
1596 
1597 	// Add program info
1598 	for (unsigned i = 0; i < context->nb_programs; i++) {
1599 		BMessage programData;
1600 		avdictionary_to_message(context->programs[i]->metadata, &programData);
1601 		_data->AddMessage("be:program", &programData);
1602 	}
1603 
1604 	return B_OK;
1605 }
1606 
1607 
1608 // #pragma mark -
1609 
1610 
1611 status_t
1612 AVFormatReader::AllocateCookie(int32 streamIndex, void** _cookie)
1613 {
1614 	TRACE("AVFormatReader::AllocateCookie(%ld)\n", streamIndex);
1615 
1616 	BAutolock _(fSourceLock);
1617 
1618 	if (fStreams == NULL)
1619 		return B_NO_INIT;
1620 
1621 	if (streamIndex < 0 || streamIndex >= fStreams[0]->CountStreams())
1622 		return B_BAD_INDEX;
1623 
1624 	if (_cookie == NULL)
1625 		return B_BAD_VALUE;
1626 
1627 	Stream* cookie = fStreams[streamIndex];
1628 	if (cookie == NULL) {
1629 		// Allocate the cookie
1630 		BMediaIO* source = dynamic_cast<BMediaIO*>(Source());
1631 		if (source == NULL) {
1632 			TRACE("  not a BMediaIO, but we need it to be one.\n");
1633 			return B_NOT_SUPPORTED;
1634 		}
1635 
1636 		cookie = new(std::nothrow) Stream(source, &fSourceLock);
1637 		if (cookie == NULL) {
1638 			ERROR("AVFormatReader::Sniff() - failed to allocate "
1639 				"Stream\n");
1640 			return B_NO_MEMORY;
1641 		}
1642 
1643 		status_t ret = cookie->Open();
1644 		if (ret != B_OK) {
1645 			TRACE("  stream failed to open: %s\n", strerror(ret));
1646 			delete cookie;
1647 			return ret;
1648 		}
1649 	}
1650 
1651 	status_t ret = cookie->Init(streamIndex);
1652 	if (ret != B_OK) {
1653 		TRACE("  stream failed to initialize: %s\n", strerror(ret));
1654 		// NOTE: Never delete the first stream!
1655 		if (streamIndex != 0)
1656 			delete cookie;
1657 		return ret;
1658 	}
1659 
1660 	fStreams[streamIndex] = cookie;
1661 	*_cookie = cookie;
1662 
1663 	return B_OK;
1664 }
1665 
1666 
1667 status_t
1668 AVFormatReader::FreeCookie(void *_cookie)
1669 {
1670 	BAutolock _(fSourceLock);
1671 
1672 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1673 
1674 	// NOTE: Never delete the first cookie!
1675 	if (cookie != NULL && cookie->VirtualIndex() != 0) {
1676 		if (fStreams != NULL)
1677 			fStreams[cookie->VirtualIndex()] = NULL;
1678 		delete cookie;
1679 	}
1680 
1681 	return B_OK;
1682 }
1683 
1684 
1685 // #pragma mark -
1686 
1687 
1688 status_t
1689 AVFormatReader::GetStreamInfo(void* _cookie, int64* frameCount,
1690 	bigtime_t* duration, media_format* format, const void** infoBuffer,
1691 	size_t* infoSize)
1692 {
1693 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1694 	return cookie->GetStreamInfo(frameCount, duration, format, infoBuffer,
1695 		infoSize);
1696 }
1697 
1698 
1699 status_t
1700 AVFormatReader::GetStreamMetaData(void* _cookie, BMessage* _data)
1701 {
1702 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1703 	return cookie->GetMetaData(_data);
1704 }
1705 
1706 
1707 status_t
1708 AVFormatReader::Seek(void* _cookie, uint32 seekTo, int64* frame,
1709 	bigtime_t* time)
1710 {
1711 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1712 	return cookie->Seek(seekTo, frame, time);
1713 }
1714 
1715 
1716 status_t
1717 AVFormatReader::FindKeyFrame(void* _cookie, uint32 flags, int64* frame,
1718 	bigtime_t* time)
1719 {
1720 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1721 	return cookie->FindKeyFrame(flags, frame, time);
1722 }
1723 
1724 
1725 status_t
1726 AVFormatReader::GetNextChunk(void* _cookie, const void** chunkBuffer,
1727 	size_t* chunkSize, media_header* mediaHeader)
1728 {
1729 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1730 	return cookie->GetNextChunk(chunkBuffer, chunkSize, mediaHeader);
1731 }
1732