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