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