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