xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVFormatReader.cpp (revision 2a2e7ad562841be14b2d1f8ad870780f32be2b1f)
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 static int
947 get_channel_count(AVCodecParameters* context)
948 {
949 #if LIBAVCODEC_VERSION_MAJOR >= 60
950 	return context->ch_layout.nb_channels;
951 #else
952 	return context->channels;
953 #endif
954 }
955 
956 
957 static int
958 get_channel_mask(AVCodecParameters* context)
959 {
960 #if LIBAVCODEC_VERSION_MAJOR >= 60
961 	return context->ch_layout.u.mask;
962 #else
963 	return context->channel_layout;
964 #endif
965 }
966 
967 
968 status_t
969 AVFormatReader::Stream::Init(int32 virtualIndex)
970 {
971 	TRACE("AVFormatReader::Stream::Init(%ld)\n", virtualIndex);
972 
973 	status_t ret = StreamBase::Init(virtualIndex);
974 	if (ret != B_OK)
975 		return ret;
976 
977 	// Get a pointer to the AVCodecPaarameters for the stream at streamIndex.
978 	AVCodecParameters* codecParams = fStream->codecpar;
979 
980 	// initialize the media_format for this stream
981 	media_format* format = &fFormat;
982 	format->Clear();
983 
984 	media_format_description description;
985 
986 	// Set format family and type depending on codec_type of the stream.
987 	switch (codecParams->codec_type) {
988 		case AVMEDIA_TYPE_AUDIO:
989 			if ((codecParams->codec_id >= AV_CODEC_ID_PCM_S16LE)
990 				&& (codecParams->codec_id <= AV_CODEC_ID_PCM_U8)) {
991 				TRACE("  raw audio\n");
992 				format->type = B_MEDIA_RAW_AUDIO;
993 				description.family = B_ANY_FORMAT_FAMILY;
994 				// This will then apparently be handled by the (built into
995 				// BMediaTrack) RawDecoder.
996 			} else {
997 				TRACE("  encoded audio\n");
998 				format->type = B_MEDIA_ENCODED_AUDIO;
999 				description.family = B_MISC_FORMAT_FAMILY;
1000 				description.u.misc.file_format = 'ffmp';
1001 			}
1002 			break;
1003 		case AVMEDIA_TYPE_VIDEO:
1004 			TRACE("  encoded video\n");
1005 			format->type = B_MEDIA_ENCODED_VIDEO;
1006 			description.family = B_MISC_FORMAT_FAMILY;
1007 			description.u.misc.file_format = 'ffmp';
1008 			break;
1009 		default:
1010 			TRACE("  unknown type\n");
1011 			format->type = B_MEDIA_UNKNOWN_TYPE;
1012 			return B_ERROR;
1013 			break;
1014 	}
1015 
1016 	if (format->type == B_MEDIA_RAW_AUDIO) {
1017 		// We cannot describe all raw-audio formats, some are unsupported.
1018 		switch (codecParams->codec_id) {
1019 			case AV_CODEC_ID_PCM_S16LE:
1020 				format->u.raw_audio.format
1021 					= media_raw_audio_format::B_AUDIO_SHORT;
1022 				format->u.raw_audio.byte_order
1023 					= B_MEDIA_LITTLE_ENDIAN;
1024 				break;
1025 			case AV_CODEC_ID_PCM_S16BE:
1026 				format->u.raw_audio.format
1027 					= media_raw_audio_format::B_AUDIO_SHORT;
1028 				format->u.raw_audio.byte_order
1029 					= B_MEDIA_BIG_ENDIAN;
1030 				break;
1031 			case AV_CODEC_ID_PCM_U16LE:
1032 //				format->u.raw_audio.format
1033 //					= media_raw_audio_format::B_AUDIO_USHORT;
1034 //				format->u.raw_audio.byte_order
1035 //					= B_MEDIA_LITTLE_ENDIAN;
1036 				return B_NOT_SUPPORTED;
1037 				break;
1038 			case AV_CODEC_ID_PCM_U16BE:
1039 //				format->u.raw_audio.format
1040 //					= media_raw_audio_format::B_AUDIO_USHORT;
1041 //				format->u.raw_audio.byte_order
1042 //					= B_MEDIA_BIG_ENDIAN;
1043 				return B_NOT_SUPPORTED;
1044 				break;
1045 			case AV_CODEC_ID_PCM_S8:
1046 				format->u.raw_audio.format
1047 					= media_raw_audio_format::B_AUDIO_CHAR;
1048 				break;
1049 			case AV_CODEC_ID_PCM_U8:
1050 				format->u.raw_audio.format
1051 					= media_raw_audio_format::B_AUDIO_UCHAR;
1052 				break;
1053 			default:
1054 				return B_NOT_SUPPORTED;
1055 				break;
1056 		}
1057 	} else {
1058 		if (description.family == B_MISC_FORMAT_FAMILY)
1059 			description.u.misc.codec = codecParams->codec_id;
1060 
1061 		BMediaFormats formats;
1062 		status_t status = formats.GetFormatFor(description, format);
1063 		if (status < B_OK)
1064 			TRACE("  formats.GetFormatFor() error: %s\n", strerror(status));
1065 
1066 		format->user_data_type = B_CODEC_TYPE_INFO;
1067 		*(uint32*)format->user_data = codecParams->codec_tag;
1068 		format->user_data[4] = 0;
1069 	}
1070 
1071 	format->require_flags = 0;
1072 	format->deny_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
1073 
1074 	switch (format->type) {
1075 		case B_MEDIA_RAW_AUDIO:
1076 			format->u.raw_audio.frame_rate = (float)codecParams->sample_rate;
1077 			format->u.raw_audio.channel_count = get_channel_count(codecParams);
1078 			format->u.raw_audio.channel_mask = get_channel_mask(codecParams);
1079 			ConvertAVSampleFormatToRawAudioFormat(
1080 				(AVSampleFormat)codecParams->format,
1081 				format->u.raw_audio.format);
1082 			format->u.raw_audio.buffer_size = 0;
1083 
1084 			// Read one packet and mark it for later re-use. (So our first
1085 			// GetNextChunk() call does not read another packet.)
1086 			if (_NextPacket(true) == B_OK) {
1087 				TRACE("  successfully determined audio buffer size: %d\n",
1088 					fPacket.size);
1089 				format->u.raw_audio.buffer_size = fPacket.size;
1090 			}
1091 			break;
1092 
1093 		case B_MEDIA_ENCODED_AUDIO:
1094 			format->u.encoded_audio.bit_rate = codecParams->bit_rate;
1095 			format->u.encoded_audio.frame_size = codecParams->frame_size;
1096 			// Fill in some info about possible output format
1097 			format->u.encoded_audio.output
1098 				= media_multi_audio_format::wildcard;
1099 			format->u.encoded_audio.output.frame_rate
1100 				= (float)codecParams->sample_rate;
1101 			// Channel layout bits match in Be API and FFmpeg.
1102 			format->u.encoded_audio.output.channel_count = get_channel_count(codecParams);
1103 			format->u.encoded_audio.multi_info.channel_mask = get_channel_mask(codecParams);
1104 			format->u.encoded_audio.output.byte_order
1105 				= avformat_to_beos_byte_order(
1106 					(AVSampleFormat)codecParams->format);
1107 
1108 			ConvertAVSampleFormatToRawAudioFormat(
1109 					(AVSampleFormat)codecParams->format,
1110 				format->u.encoded_audio.output.format);
1111 
1112 			if (codecParams->block_align > 0) {
1113 				format->u.encoded_audio.output.buffer_size
1114 					= codecParams->block_align;
1115 			} else {
1116 				format->u.encoded_audio.output.buffer_size
1117 					= codecParams->frame_size * get_channel_count(codecParams)
1118 						* (format->u.encoded_audio.output.format
1119 							& media_raw_audio_format::B_AUDIO_SIZE_MASK);
1120 			}
1121 			break;
1122 
1123 		case B_MEDIA_ENCODED_VIDEO:
1124 // TODO: Specifying any of these seems to throw off the format matching
1125 // later on.
1126 //			format->u.encoded_video.avg_bit_rate = codecParams->bit_rate;
1127 //			format->u.encoded_video.max_bit_rate = codecParams->bit_rate
1128 //				+ codecParams->bit_rate_tolerance;
1129 
1130 //			format->u.encoded_video.encoding
1131 //				= media_encoded_video_format::B_ANY;
1132 
1133 //			format->u.encoded_video.frame_size = 1;
1134 //			format->u.encoded_video.forward_history = 0;
1135 //			format->u.encoded_video.backward_history = 0;
1136 
1137 			format->u.encoded_video.output.field_rate = FrameRate();
1138 			format->u.encoded_video.output.interlace = 1;
1139 
1140 			format->u.encoded_video.output.first_active = 0;
1141 			format->u.encoded_video.output.last_active
1142 				= codecParams->height - 1;
1143 				// TODO: Maybe libavformat actually provides that info
1144 				// somewhere...
1145 			format->u.encoded_video.output.orientation
1146 				= B_VIDEO_TOP_LEFT_RIGHT;
1147 
1148 			ConvertAVCodecParametersToVideoAspectWidthAndHeight(*codecParams,
1149 				format->u.encoded_video.output.pixel_width_aspect,
1150 				format->u.encoded_video.output.pixel_height_aspect);
1151 
1152 			format->u.encoded_video.output.display.format
1153 				= pixfmt_to_colorspace(codecParams->format);
1154 			format->u.encoded_video.output.display.line_width
1155 				= codecParams->width;
1156 			format->u.encoded_video.output.display.line_count
1157 				= codecParams->height;
1158 			TRACE("  width/height: %d/%d\n", codecParams->width,
1159 				codecParams->height);
1160 			format->u.encoded_video.output.display.bytes_per_row = 0;
1161 			format->u.encoded_video.output.display.pixel_offset = 0;
1162 			format->u.encoded_video.output.display.line_offset = 0;
1163 			format->u.encoded_video.output.display.flags = 0; // TODO
1164 
1165 			break;
1166 
1167 		default:
1168 			// This is an unknown format to us.
1169 			break;
1170 	}
1171 
1172 	// Add the meta data, if any
1173 	if (codecParams->extradata_size > 0) {
1174 		format->SetMetaData(codecParams->extradata,
1175 			codecParams->extradata_size);
1176 		TRACE("  extradata: %p\n", format->MetaData());
1177 	}
1178 
1179 	TRACE("  extradata_size: %d\n", codecParams->extradata_size);
1180 //	TRACE("  intra_matrix: %p\n", codecParams->intra_matrix);
1181 //	TRACE("  inter_matrix: %p\n", codecParams->inter_matrix);
1182 //	TRACE("  get_buffer(): %p\n", codecParams->get_buffer);
1183 //	TRACE("  release_buffer(): %p\n", codecParams->release_buffer);
1184 
1185 #ifdef TRACE_AVFORMAT_READER
1186 	char formatString[512];
1187 	if (string_for_format(*format, formatString, sizeof(formatString)))
1188 		TRACE("  format: %s\n", formatString);
1189 
1190 	uint32 encoding = format->Encoding();
1191 	TRACE("  encoding '%.4s'\n", (char*)&encoding);
1192 #endif
1193 
1194 	return B_OK;
1195 }
1196 
1197 
1198 status_t
1199 AVFormatReader::Stream::GetMetaData(BMessage* data)
1200 {
1201 	BAutolock _(&fLock);
1202 
1203 	avdictionary_to_message(fStream->metadata, data);
1204 
1205 	return B_OK;
1206 }
1207 
1208 
1209 status_t
1210 AVFormatReader::Stream::GetStreamInfo(int64* frameCount,
1211 	bigtime_t* duration, media_format* format, const void** infoBuffer,
1212 	size_t* infoSize) const
1213 {
1214 	BAutolock _(&fLock);
1215 
1216 	TRACE("AVFormatReader::Stream::GetStreamInfo(%ld)\n",
1217 		VirtualIndex());
1218 
1219 	double frameRate = FrameRate();
1220 	TRACE("  frameRate: %.4f\n", frameRate);
1221 
1222 	#ifdef TRACE_AVFORMAT_READER
1223 	if (fStream->start_time != AV_NOPTS_VALUE) {
1224 		bigtime_t startTime = _ConvertFromStreamTimeBase(fStream->start_time);
1225 		TRACE("  start_time: %lld or %.5fs\n", startTime,
1226 			startTime / 1000000.0);
1227 		// TODO: Handle start time in FindKeyFrame() and Seek()?!
1228 	}
1229 	#endif // TRACE_AVFORMAT_READER
1230 
1231 	*duration = Duration();
1232 
1233 	TRACE("  duration: %lld or %.5fs\n", *duration, *duration / 1000000.0);
1234 
1235 	#if 0
1236 	if (fStream->nb_index_entries > 0) {
1237 		TRACE("  dump of index entries:\n");
1238 		int count = 5;
1239 		int firstEntriesCount = min_c(fStream->nb_index_entries, count);
1240 		int i = 0;
1241 		for (; i < firstEntriesCount; i++) {
1242 			AVIndexEntry& entry = fStream->index_entries[i];
1243 			bigtime_t timeGlobal = entry.timestamp;
1244 			bigtime_t timeNative = _ConvertFromStreamTimeBase(timeGlobal);
1245 			TRACE("    [%d] native: %.5fs global: %.5fs\n", i,
1246 				timeNative / 1000000.0f, timeGlobal / 1000000.0f);
1247 		}
1248 		if (fStream->nb_index_entries - count > i) {
1249 			i = fStream->nb_index_entries - count;
1250 			TRACE("    ...\n");
1251 			for (; i < fStream->nb_index_entries; i++) {
1252 				AVIndexEntry& entry = fStream->index_entries[i];
1253 				bigtime_t timeGlobal = entry.timestamp;
1254 				bigtime_t timeNative = _ConvertFromStreamTimeBase(timeGlobal);
1255 				TRACE("    [%d] native: %.5fs global: %.5fs\n", i,
1256 					timeNative / 1000000.0f, timeGlobal / 1000000.0f);
1257 			}
1258 		}
1259 	}
1260 	#endif
1261 
1262 	*frameCount = fStream->nb_frames * fStream->codecpar->frame_size;
1263 	if (*frameCount == 0) {
1264 		// Calculate from duration and frame rate
1265 		*frameCount = (int64)(*duration * frameRate / 1000000LL);
1266 		TRACE("  frameCount calculated: %lld, from context: %lld\n",
1267 			*frameCount, fStream->nb_frames);
1268 	} else
1269 		TRACE("  frameCount: %lld\n", *frameCount);
1270 
1271 	*format = fFormat;
1272 
1273 	*infoBuffer = fStream->codecpar->extradata;
1274 	*infoSize = fStream->codecpar->extradata_size;
1275 
1276 	return B_OK;
1277 }
1278 
1279 
1280 status_t
1281 AVFormatReader::Stream::FindKeyFrame(uint32 flags, int64* frame,
1282 	bigtime_t* time) const
1283 {
1284 	BAutolock _(&fLock);
1285 
1286 	if (fContext == NULL || fStream == NULL)
1287 		return B_NO_INIT;
1288 
1289 	TRACE_FIND("AVFormatReader::Stream::FindKeyFrame(%ld,%s%s%s%s, "
1290 		"%lld, %lld)\n", VirtualIndex(),
1291 		(flags & B_MEDIA_SEEK_TO_FRAME) ? " B_MEDIA_SEEK_TO_FRAME" : "",
1292 		(flags & B_MEDIA_SEEK_TO_TIME) ? " B_MEDIA_SEEK_TO_TIME" : "",
1293 		(flags & B_MEDIA_SEEK_CLOSEST_BACKWARD)
1294 			? " B_MEDIA_SEEK_CLOSEST_BACKWARD" : "",
1295 		(flags & B_MEDIA_SEEK_CLOSEST_FORWARD)
1296 			? " B_MEDIA_SEEK_CLOSEST_FORWARD" : "",
1297 		*frame, *time);
1298 
1299 	bool inLastRequestedRange = false;
1300 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1301 		if (fLastReportedKeyframe.reportedFrame
1302 			<= fLastReportedKeyframe.requestedFrame) {
1303 			inLastRequestedRange
1304 				= *frame >= fLastReportedKeyframe.reportedFrame
1305 					&& *frame <= fLastReportedKeyframe.requestedFrame;
1306 		} else {
1307 			inLastRequestedRange
1308 				= *frame >= fLastReportedKeyframe.requestedFrame
1309 					&& *frame <= fLastReportedKeyframe.reportedFrame;
1310 		}
1311 	} else if ((flags & B_MEDIA_SEEK_TO_FRAME) == 0) {
1312 		if (fLastReportedKeyframe.reportedTime
1313 			<= fLastReportedKeyframe.requestedTime) {
1314 			inLastRequestedRange
1315 				= *time >= fLastReportedKeyframe.reportedTime
1316 					&& *time <= fLastReportedKeyframe.requestedTime;
1317 		} else {
1318 			inLastRequestedRange
1319 				= *time >= fLastReportedKeyframe.requestedTime
1320 					&& *time <= fLastReportedKeyframe.reportedTime;
1321 		}
1322 	}
1323 
1324 	if (inLastRequestedRange) {
1325 		*frame = fLastReportedKeyframe.reportedFrame;
1326 		*time = fLastReportedKeyframe.reportedTime;
1327 		TRACE_FIND("  same as last reported keyframe\n");
1328 		return B_OK;
1329 	}
1330 
1331 	double frameRate = FrameRate();
1332 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0)
1333 		*time = (bigtime_t)(*frame * 1000000.0 / frameRate + 0.5);
1334 
1335 	status_t ret;
1336 	if (fGhostStream == NULL) {
1337 		BAutolock _(fSourceLock);
1338 
1339 		fGhostStream = new(std::nothrow) StreamBase(fSource, fSourceLock,
1340 			&fLock);
1341 		if (fGhostStream == NULL) {
1342 			TRACE("  failed to allocate ghost stream\n");
1343 			return B_NO_MEMORY;
1344 		}
1345 
1346 		ret = fGhostStream->Open();
1347 		if (ret != B_OK) {
1348 			TRACE("  ghost stream failed to open: %s\n", strerror(ret));
1349 			return B_ERROR;
1350 		}
1351 
1352 		ret = fGhostStream->Init(fVirtualIndex);
1353 		if (ret != B_OK) {
1354 			TRACE("  ghost stream failed to init: %s\n", strerror(ret));
1355 			return B_ERROR;
1356 		}
1357 	}
1358 	fLastReportedKeyframe.requestedFrame = *frame;
1359 	fLastReportedKeyframe.requestedTime = *time;
1360 	fLastReportedKeyframe.seekFlags = flags;
1361 
1362 	ret = fGhostStream->Seek(flags, frame, time);
1363 	if (ret != B_OK) {
1364 		TRACE("  ghost stream failed to seek: %s\n", strerror(ret));
1365 		return B_ERROR;
1366 	}
1367 
1368 	fLastReportedKeyframe.reportedFrame = *frame;
1369 	fLastReportedKeyframe.reportedTime = *time;
1370 
1371 	TRACE_FIND("  found time: %.2fs\n", *time / 1000000.0);
1372 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1373 		*frame = int64_t(*time * FrameRate() / 1000000.0 + 0.5);
1374 		TRACE_FIND("  found frame: %lld\n", *frame);
1375 	}
1376 
1377 	return B_OK;
1378 }
1379 
1380 
1381 status_t
1382 AVFormatReader::Stream::Seek(uint32 flags, int64* frame, bigtime_t* time)
1383 {
1384 	BAutolock _(&fLock);
1385 
1386 	if (fContext == NULL || fStream == NULL)
1387 		return B_NO_INIT;
1388 
1389 	// Put the old requested values into frame/time, since we already know
1390 	// that the sought frame/time will then match the reported values.
1391 	// TODO: Will not work if client changes seek flags (from backwards to
1392 	// forward or vice versa)!!
1393 	bool inLastRequestedRange = false;
1394 	if ((flags & B_MEDIA_SEEK_TO_FRAME) != 0) {
1395 		if (fLastReportedKeyframe.reportedFrame
1396 			<= fLastReportedKeyframe.requestedFrame) {
1397 			inLastRequestedRange
1398 				= *frame >= fLastReportedKeyframe.reportedFrame
1399 					&& *frame <= fLastReportedKeyframe.requestedFrame;
1400 		} else {
1401 			inLastRequestedRange
1402 				= *frame >= fLastReportedKeyframe.requestedFrame
1403 					&& *frame <= fLastReportedKeyframe.reportedFrame;
1404 		}
1405 	} else if ((flags & B_MEDIA_SEEK_TO_FRAME) == 0) {
1406 		if (fLastReportedKeyframe.reportedTime
1407 			<= fLastReportedKeyframe.requestedTime) {
1408 			inLastRequestedRange
1409 				= *time >= fLastReportedKeyframe.reportedTime
1410 					&& *time <= fLastReportedKeyframe.requestedTime;
1411 		} else {
1412 			inLastRequestedRange
1413 				= *time >= fLastReportedKeyframe.requestedTime
1414 					&& *time <= fLastReportedKeyframe.reportedTime;
1415 		}
1416 	}
1417 
1418 	if (inLastRequestedRange) {
1419 		*frame = fLastReportedKeyframe.requestedFrame;
1420 		*time = fLastReportedKeyframe.requestedTime;
1421 		flags = fLastReportedKeyframe.seekFlags;
1422 	}
1423 
1424 	return StreamBase::Seek(flags, frame, time);
1425 }
1426 
1427 
1428 // #pragma mark - AVFormatReader
1429 
1430 
1431 AVFormatReader::AVFormatReader()
1432 	:
1433 	fCopyright(""),
1434 	fStreams(NULL),
1435 	fSourceLock("source I/O lock")
1436 {
1437 	TRACE("AVFormatReader::AVFormatReader\n");
1438 }
1439 
1440 
1441 AVFormatReader::~AVFormatReader()
1442 {
1443 	TRACE("AVFormatReader::~AVFormatReader\n");
1444 	if (fStreams != NULL) {
1445 		// The client was supposed to call FreeCookie() on all
1446 		// allocated streams. Deleting the first stream is always
1447 		// prevented, we delete the other ones just in case.
1448 		int32 count = fStreams[0]->CountStreams();
1449 		for (int32 i = 0; i < count; i++)
1450 			delete fStreams[i];
1451 		delete[] fStreams;
1452 	}
1453 }
1454 
1455 
1456 // #pragma mark -
1457 
1458 
1459 const char*
1460 AVFormatReader::Copyright()
1461 {
1462 	if (fCopyright.Length() <= 0) {
1463 		BMessage message;
1464 		if (GetMetaData(&message) == B_OK)
1465 			message.FindString("copyright", &fCopyright);
1466 	}
1467 	return fCopyright.String();
1468 }
1469 
1470 
1471 status_t
1472 AVFormatReader::Sniff(int32* _streamCount)
1473 {
1474 	TRACE("AVFormatReader::Sniff\n");
1475 
1476 	BMediaIO* source = dynamic_cast<BMediaIO*>(Source());
1477 	if (source == NULL) {
1478 		TRACE("  not a BMediaIO, but we need it to be one.\n");
1479 		return B_NOT_SUPPORTED;
1480 	}
1481 
1482 	Stream* stream = new(std::nothrow) Stream(source,
1483 		&fSourceLock);
1484 	if (stream == NULL) {
1485 		ERROR("AVFormatReader::Sniff() - failed to allocate Stream\n");
1486 		return B_NO_MEMORY;
1487 	}
1488 
1489 	ObjectDeleter<Stream> streamDeleter(stream);
1490 
1491 	status_t ret = stream->Open();
1492 	if (ret != B_OK) {
1493 		TRACE("  failed to detect stream: %s\n", strerror(ret));
1494 		return ret;
1495 	}
1496 
1497 	delete[] fStreams;
1498 	fStreams = NULL;
1499 
1500 	int32 streamCount = stream->CountStreams();
1501 	if (streamCount == 0) {
1502 		TRACE("  failed to detect any streams: %s\n", strerror(ret));
1503 		return B_ERROR;
1504 	}
1505 
1506 	fStreams = new(std::nothrow) Stream*[streamCount];
1507 	if (fStreams == NULL) {
1508 		ERROR("AVFormatReader::Sniff() - failed to allocate streams\n");
1509 		return B_NO_MEMORY;
1510 	}
1511 
1512 	memset(fStreams, 0, sizeof(Stream*) * streamCount);
1513 	fStreams[0] = stream;
1514 	streamDeleter.Detach();
1515 
1516 	#ifdef TRACE_AVFORMAT_READER
1517 	av_dump_format(const_cast<AVFormatContext*>(stream->Context()), 0, "", 0);
1518 	#endif
1519 
1520 	if (_streamCount != NULL)
1521 		*_streamCount = streamCount;
1522 
1523 	return B_OK;
1524 }
1525 
1526 
1527 void
1528 AVFormatReader::GetFileFormatInfo(media_file_format* mff)
1529 {
1530 	TRACE("AVFormatReader::GetFileFormatInfo\n");
1531 
1532 	if (fStreams == NULL)
1533 		return;
1534 
1535 	// The first cookie is always there!
1536 	const AVFormatContext* context = fStreams[0]->Context();
1537 
1538 	if (context == NULL || context->iformat == NULL) {
1539 		TRACE("  no AVFormatContext or AVInputFormat!\n");
1540 		return;
1541 	}
1542 
1543 	const media_file_format* format = demuxer_format_for(context->iformat);
1544 
1545 	mff->capabilities = media_file_format::B_READABLE
1546 		| media_file_format::B_KNOWS_ENCODED_VIDEO
1547 		| media_file_format::B_KNOWS_ENCODED_AUDIO
1548 		| media_file_format::B_IMPERFECTLY_SEEKABLE;
1549 
1550 	if (format != NULL) {
1551 		mff->family = format->family;
1552 	} else {
1553 		TRACE("  no DemuxerFormat for AVInputFormat!\n");
1554 		mff->family = B_MISC_FORMAT_FAMILY;
1555 	}
1556 
1557 	mff->version = 100;
1558 
1559 	if (format != NULL) {
1560 		strlcpy(mff->mime_type, format->mime_type, sizeof(mff->mime_type));
1561 	} else {
1562 		// TODO: Would be nice to be able to provide this from AVInputFormat,
1563 		// maybe by extending the FFmpeg code itself (all demuxers).
1564 		mff->mime_type[0] = '\0';
1565 	}
1566 
1567 	if (context->iformat->extensions != NULL)
1568 		strlcpy(mff->file_extension, context->iformat->extensions, sizeof(mff->file_extension));
1569 	else {
1570 		TRACE("  no file extensions for AVInputFormat.\n");
1571 		mff->file_extension[0] = '\0';
1572 	}
1573 
1574 	if (context->iformat->name != NULL)
1575 		strlcpy(mff->short_name,  context->iformat->name, sizeof(mff->short_name));
1576 	else {
1577 		TRACE("  no short name for AVInputFormat.\n");
1578 		mff->short_name[0] = '\0';
1579 	}
1580 
1581 	if (context->iformat->long_name != NULL) {
1582 		snprintf(mff->pretty_name, sizeof(mff->pretty_name), "%s (FFmpeg)",
1583 			context->iformat->long_name);
1584 	} else if (format != NULL)
1585 		snprintf(mff->pretty_name, sizeof(mff->pretty_name), "%.54s (FFmpeg)", format->pretty_name);
1586 	else
1587 		strlcpy(mff->pretty_name, "Unknown (FFmpeg)", sizeof(mff->pretty_name));
1588 }
1589 
1590 
1591 status_t
1592 AVFormatReader::GetMetaData(BMessage* _data)
1593 {
1594 	// The first cookie is always there!
1595 	const AVFormatContext* context = fStreams[0]->Context();
1596 
1597 	if (context == NULL)
1598 		return B_NO_INIT;
1599 
1600 	avdictionary_to_message(context->metadata, _data);
1601 
1602 	// Add chapter info
1603 	for (unsigned i = 0; i < context->nb_chapters; i++) {
1604 		AVChapter* chapter = context->chapters[i];
1605 		BMessage chapterData;
1606 		chapterData.AddInt64("start", bigtime_t(1000000.0
1607 			* chapter->start * chapter->time_base.num
1608 			/ chapter->time_base.den + 0.5));
1609 		chapterData.AddInt64("end", bigtime_t(1000000.0
1610 			* chapter->end * chapter->time_base.num
1611 			/ chapter->time_base.den + 0.5));
1612 
1613 		avdictionary_to_message(chapter->metadata, &chapterData);
1614 		_data->AddMessage("be:chapter", &chapterData);
1615 	}
1616 
1617 	// Add program info
1618 	for (unsigned i = 0; i < context->nb_programs; i++) {
1619 		BMessage programData;
1620 		avdictionary_to_message(context->programs[i]->metadata, &programData);
1621 		_data->AddMessage("be:program", &programData);
1622 	}
1623 
1624 	return B_OK;
1625 }
1626 
1627 
1628 // #pragma mark -
1629 
1630 
1631 status_t
1632 AVFormatReader::AllocateCookie(int32 streamIndex, void** _cookie)
1633 {
1634 	TRACE("AVFormatReader::AllocateCookie(%ld)\n", streamIndex);
1635 
1636 	BAutolock _(fSourceLock);
1637 
1638 	if (fStreams == NULL)
1639 		return B_NO_INIT;
1640 
1641 	if (streamIndex < 0 || streamIndex >= fStreams[0]->CountStreams())
1642 		return B_BAD_INDEX;
1643 
1644 	if (_cookie == NULL)
1645 		return B_BAD_VALUE;
1646 
1647 	Stream* cookie = fStreams[streamIndex];
1648 	if (cookie == NULL) {
1649 		// Allocate the cookie
1650 		BMediaIO* source = dynamic_cast<BMediaIO*>(Source());
1651 		if (source == NULL) {
1652 			TRACE("  not a BMediaIO, but we need it to be one.\n");
1653 			return B_NOT_SUPPORTED;
1654 		}
1655 
1656 		cookie = new(std::nothrow) Stream(source, &fSourceLock);
1657 		if (cookie == NULL) {
1658 			ERROR("AVFormatReader::Sniff() - failed to allocate "
1659 				"Stream\n");
1660 			return B_NO_MEMORY;
1661 		}
1662 
1663 		status_t ret = cookie->Open();
1664 		if (ret != B_OK) {
1665 			TRACE("  stream failed to open: %s\n", strerror(ret));
1666 			delete cookie;
1667 			return ret;
1668 		}
1669 	}
1670 
1671 	status_t ret = cookie->Init(streamIndex);
1672 	if (ret != B_OK) {
1673 		TRACE("  stream failed to initialize: %s\n", strerror(ret));
1674 		// NOTE: Never delete the first stream!
1675 		if (streamIndex != 0)
1676 			delete cookie;
1677 		return ret;
1678 	}
1679 
1680 	fStreams[streamIndex] = cookie;
1681 	*_cookie = cookie;
1682 
1683 	return B_OK;
1684 }
1685 
1686 
1687 status_t
1688 AVFormatReader::FreeCookie(void *_cookie)
1689 {
1690 	BAutolock _(fSourceLock);
1691 
1692 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1693 
1694 	// NOTE: Never delete the first cookie!
1695 	if (cookie != NULL && cookie->VirtualIndex() != 0) {
1696 		if (fStreams != NULL)
1697 			fStreams[cookie->VirtualIndex()] = NULL;
1698 		delete cookie;
1699 	}
1700 
1701 	return B_OK;
1702 }
1703 
1704 
1705 // #pragma mark -
1706 
1707 
1708 status_t
1709 AVFormatReader::GetStreamInfo(void* _cookie, int64* frameCount,
1710 	bigtime_t* duration, media_format* format, const void** infoBuffer,
1711 	size_t* infoSize)
1712 {
1713 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1714 	return cookie->GetStreamInfo(frameCount, duration, format, infoBuffer,
1715 		infoSize);
1716 }
1717 
1718 
1719 status_t
1720 AVFormatReader::GetStreamMetaData(void* _cookie, BMessage* _data)
1721 {
1722 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1723 	return cookie->GetMetaData(_data);
1724 }
1725 
1726 
1727 status_t
1728 AVFormatReader::Seek(void* _cookie, uint32 seekTo, int64* frame,
1729 	bigtime_t* time)
1730 {
1731 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1732 	return cookie->Seek(seekTo, frame, time);
1733 }
1734 
1735 
1736 status_t
1737 AVFormatReader::FindKeyFrame(void* _cookie, uint32 flags, int64* frame,
1738 	bigtime_t* time)
1739 {
1740 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1741 	return cookie->FindKeyFrame(flags, frame, time);
1742 }
1743 
1744 
1745 status_t
1746 AVFormatReader::GetNextChunk(void* _cookie, const void** chunkBuffer,
1747 	size_t* chunkSize, media_header* mediaHeader)
1748 {
1749 	Stream* cookie = reinterpret_cast<Stream*>(_cookie);
1750 	return cookie->GetNextChunk(chunkBuffer, chunkSize, mediaHeader);
1751 }
1752