xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp (revision d64b771b96050fca8ff1859daa5ec44ff3493af7)
1 /*
2  * Copyright (C) 2001 Carlos Hasan
3  * Copyright (C) 2001 François Revol
4  * Copyright (C) 2001 Axel Dörfler
5  * Copyright (C) 2004 Marcus Overhagen
6  * Copyright (C) 2009 Stephan Amßus <superstippi@gmx.de>
7  * Copyright (C) 2014 Colin Günther <coling@gmx.de>
8  *
9  * All rights reserved. Distributed under the terms of the MIT License.
10  */
11 
12 //! libavcodec based decoder for Haiku
13 
14 #include "AVCodecDecoder.h"
15 
16 #include <new>
17 
18 #include <assert.h>
19 #include <string.h>
20 
21 #include <Bitmap.h>
22 #include <Debug.h>
23 
24 #include "Utilities.h"
25 
26 
27 #undef TRACE
28 //#define TRACE_AV_CODEC
29 #ifdef TRACE_AV_CODEC
30 #	define TRACE(x...)	printf(x)
31 #	define TRACE_AUDIO(x...)	printf(x)
32 #	define TRACE_VIDEO(x...)	printf(x)
33 #else
34 #	define TRACE(x...)
35 #	define TRACE_AUDIO(x...)
36 #	define TRACE_VIDEO(x...)
37 #endif
38 
39 //#define LOG_STREAM_TO_FILE
40 #ifdef LOG_STREAM_TO_FILE
41 #	include <File.h>
42 	static BFile sAudioStreamLogFile(
43 		"/boot/home/Desktop/AVCodecDebugAudioStream.raw",
44 		B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
45 	static BFile sVideoStreamLogFile(
46 		"/boot/home/Desktop/AVCodecDebugVideoStream.raw",
47 		B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
48 	static int sDumpedPackets = 0;
49 #endif
50 
51 #ifdef __x86_64
52 #define USE_SWS_FOR_COLOR_SPACE_CONVERSION 1
53 #else
54 #define USE_SWS_FOR_COLOR_SPACE_CONVERSION 0
55 // NOTE: David's color space conversion is much faster than the FFmpeg
56 // version. Perhaps the SWS code can be used for unsupported conversions?
57 // Otherwise the alternative code could simply be removed from this file.
58 #endif
59 
60 
61 struct wave_format_ex {
62 	uint16 format_tag;
63 	uint16 channels;
64 	uint32 frames_per_sec;
65 	uint32 avg_bytes_per_sec;
66 	uint16 block_align;
67 	uint16 bits_per_sample;
68 	uint16 extra_size;
69 	// extra_data[extra_size]
70 } _PACKED;
71 
72 struct avformat_codec_context {
73 	int sample_rate;
74 	int channels;
75 };
76 
77 
78 // profiling related globals
79 #define DO_PROFILING 0
80 
81 static bigtime_t decodingTime = 0;
82 static bigtime_t conversionTime = 0;
83 static long profileCounter = 0;
84 
85 
86 AVCodecDecoder::AVCodecDecoder()
87 	:
88 	fHeader(),
89 	fInputFormat(),
90 	fFrame(0),
91 	fIsAudio(false),
92 	fCodec(NULL),
93 	fContext(avcodec_alloc_context3(NULL)),
94 	fDecodedData(NULL),
95 	fDecodedDataSizeInBytes(0),
96 	fPostProcessedDecodedPicture(avcodec_alloc_frame()),
97 	fRawDecodedPicture(avcodec_alloc_frame()),
98 	fRawDecodedAudio(avcodec_alloc_frame()),
99 
100 	fCodecInitDone(false),
101 
102 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
103 	fSwsContext(NULL),
104 #else
105 	fFormatConversionFunc(NULL),
106 #endif
107 
108 	fExtraData(NULL),
109 	fExtraDataSize(0),
110 	fBlockAlign(0),
111 
112 	fOutputColorSpace(B_NO_COLOR_SPACE),
113 	fOutputFrameCount(0),
114 	fOutputFrameRate(1.0),
115 	fOutputFrameSize(0),
116 
117 	fChunkBuffer(NULL),
118 	fChunkBufferSize(0),
119 	fAudioDecodeError(false),
120 
121 	fDecodedDataBuffer(avcodec_alloc_frame()),
122 	fDecodedDataBufferOffset(0),
123 	fDecodedDataBufferSize(0)
124 {
125 	TRACE("AVCodecDecoder::AVCodecDecoder()\n");
126 
127 	system_info info;
128 	get_system_info(&info);
129 
130 	fContext->err_recognition = AV_EF_CAREFUL;
131 	fContext->error_concealment = 3;
132 	fContext->thread_count = info.cpu_count;
133 }
134 
135 
136 AVCodecDecoder::~AVCodecDecoder()
137 {
138 	TRACE("[%c] AVCodecDecoder::~AVCodecDecoder()\n", fIsAudio?('a'):('v'));
139 
140 #ifdef DO_PROFILING
141 	if (profileCounter > 0) {
142 		printf("[%c] profile: d1 = %lld, d2 = %lld (%Ld)\n",
143 			fIsAudio?('a'):('v'), decodingTime / profileCounter,
144 			conversionTime / profileCounter, fFrame);
145 	}
146 #endif
147 
148 	if (fCodecInitDone)
149 		avcodec_close(fContext);
150 
151 	free(fChunkBuffer);
152 	free(fDecodedData);
153 
154 	av_free(fPostProcessedDecodedPicture);
155 	av_free(fRawDecodedPicture);
156 	av_free(fRawDecodedAudio->opaque);
157 	av_free(fRawDecodedAudio);
158 	av_free(fContext);
159 	av_free(fDecodedDataBuffer);
160 
161 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
162 	if (fSwsContext != NULL)
163 		sws_freeContext(fSwsContext);
164 #endif
165 
166 	delete[] fExtraData;
167 }
168 
169 
170 void
171 AVCodecDecoder::GetCodecInfo(media_codec_info* mci)
172 {
173 	snprintf(mci->short_name, 32, "%s", fCodec->name);
174 	snprintf(mci->pretty_name, 96, "%s", fCodec->long_name);
175 	mci->id = 0;
176 	mci->sub_id = fCodec->id;
177 }
178 
179 
180 status_t
181 AVCodecDecoder::Setup(media_format* ioEncodedFormat, const void* infoBuffer,
182 	size_t infoSize)
183 {
184 	if (ioEncodedFormat->type != B_MEDIA_ENCODED_AUDIO
185 		&& ioEncodedFormat->type != B_MEDIA_ENCODED_VIDEO)
186 		return B_ERROR;
187 
188 	fIsAudio = (ioEncodedFormat->type == B_MEDIA_ENCODED_AUDIO);
189 	TRACE("[%c] AVCodecDecoder::Setup()\n", fIsAudio?('a'):('v'));
190 
191 #ifdef TRACE_AV_CODEC
192 	char buffer[1024];
193 	string_for_format(*ioEncodedFormat, buffer, sizeof(buffer));
194 	TRACE("[%c]   input_format = %s\n", fIsAudio?('a'):('v'), buffer);
195 	TRACE("[%c]   infoSize = %ld\n", fIsAudio?('a'):('v'), infoSize);
196 	TRACE("[%c]   user_data_type = %08lx\n", fIsAudio?('a'):('v'),
197 		ioEncodedFormat->user_data_type);
198 	TRACE("[%c]   meta_data_size = %ld\n", fIsAudio?('a'):('v'),
199 		ioEncodedFormat->MetaDataSize());
200 #endif
201 
202 	media_format_description description;
203 	if (BMediaFormats().GetCodeFor(*ioEncodedFormat,
204 			B_MISC_FORMAT_FAMILY, &description) == B_OK) {
205 		if (description.u.misc.file_format != 'ffmp')
206 			return B_NOT_SUPPORTED;
207 		fCodec = avcodec_find_decoder(static_cast<CodecID>(
208 			description.u.misc.codec));
209 		if (fCodec == NULL) {
210 			TRACE("  unable to find the correct FFmpeg "
211 				"decoder (id = %lu)\n", description.u.misc.codec);
212 			return B_ERROR;
213 		}
214 		TRACE("  found decoder %s\n", fCodec->name);
215 
216 		const void* extraData = infoBuffer;
217 		fExtraDataSize = infoSize;
218 		if (description.family == B_WAV_FORMAT_FAMILY
219 				&& infoSize >= sizeof(wave_format_ex)) {
220 			TRACE("  trying to use wave_format_ex\n");
221 			// Special case extra data in B_WAV_FORMAT_FAMILY
222 			const wave_format_ex* waveFormatData
223 				= (const wave_format_ex*)infoBuffer;
224 
225 			size_t waveFormatSize = infoSize;
226 			if (waveFormatData != NULL && waveFormatSize > 0) {
227 				fBlockAlign = waveFormatData->block_align;
228 				TRACE("  found block align: %d\n", fBlockAlign);
229 				fExtraDataSize = waveFormatData->extra_size;
230 				// skip the wave_format_ex from the extra data.
231 				extraData = waveFormatData + 1;
232 			}
233 		} else {
234 			if (fIsAudio) {
235 				fBlockAlign
236 					= ioEncodedFormat->u.encoded_audio.output
237 						.buffer_size;
238 				TRACE("  using buffer_size as block align: %d\n",
239 					fBlockAlign);
240 			}
241 		}
242 		if (extraData != NULL && fExtraDataSize > 0) {
243 			TRACE("AVCodecDecoder: extra data size %ld\n", infoSize);
244 			delete[] fExtraData;
245 			fExtraData = new(std::nothrow) char[fExtraDataSize];
246 			if (fExtraData != NULL)
247 				memcpy(fExtraData, infoBuffer, fExtraDataSize);
248 			else
249 				fExtraDataSize = 0;
250 		}
251 
252 		fInputFormat = *ioEncodedFormat;
253 		return B_OK;
254 	} else {
255 		TRACE("AVCodecDecoder: BMediaFormats().GetCodeFor() failed.\n");
256 	}
257 
258 	printf("AVCodecDecoder::Setup failed!\n");
259 	return B_ERROR;
260 }
261 
262 
263 status_t
264 AVCodecDecoder::SeekedTo(int64 frame, bigtime_t time)
265 {
266 	status_t ret = B_OK;
267 	// Reset the FFmpeg codec to flush buffers, so we keep the sync
268 	if (fCodecInitDone) {
269 		avcodec_flush_buffers(fContext);
270 		_ResetTempPacket();
271 	}
272 
273 	// Flush internal buffers as well.
274 	free(fChunkBuffer);
275 	fChunkBuffer = NULL;
276 	fChunkBufferSize = 0;
277 	fDecodedDataBufferOffset = 0;
278 	fDecodedDataBufferSize = 0;
279 	fDecodedDataSizeInBytes = 0;
280 
281 	fFrame = frame;
282 
283 	return ret;
284 }
285 
286 
287 status_t
288 AVCodecDecoder::NegotiateOutputFormat(media_format* inOutFormat)
289 {
290 	TRACE("AVCodecDecoder::NegotiateOutputFormat() [%c] \n",
291 		fIsAudio?('a'):('v'));
292 
293 #ifdef TRACE_AV_CODEC
294 	char buffer[1024];
295 	string_for_format(*inOutFormat, buffer, sizeof(buffer));
296 	TRACE("  [%c]  requested format = %s\n", fIsAudio?('a'):('v'), buffer);
297 #endif
298 
299 	if (fIsAudio)
300 		return _NegotiateAudioOutputFormat(inOutFormat);
301 	else
302 		return _NegotiateVideoOutputFormat(inOutFormat);
303 }
304 
305 
306 status_t
307 AVCodecDecoder::Decode(void* outBuffer, int64* outFrameCount,
308 	media_header* mediaHeader, media_decode_info* info)
309 {
310 	if (!fCodecInitDone)
311 		return B_NO_INIT;
312 
313 	status_t ret;
314 	if (fIsAudio)
315 		ret = _DecodeAudio(outBuffer, outFrameCount, mediaHeader, info);
316 	else
317 		ret = _DecodeVideo(outBuffer, outFrameCount, mediaHeader, info);
318 
319 	return ret;
320 }
321 
322 
323 // #pragma mark -
324 
325 
326 void
327 AVCodecDecoder::_ResetTempPacket()
328 {
329 	av_init_packet(&fTempPacket);
330 	fTempPacket.size = 0;
331 	fTempPacket.data = NULL;
332 }
333 
334 
335 status_t
336 AVCodecDecoder::_NegotiateAudioOutputFormat(media_format* inOutFormat)
337 {
338 	TRACE("AVCodecDecoder::_NegotiateAudioOutputFormat()\n");
339 
340 	_ApplyEssentialAudioContainerPropertiesToContext();
341 		// This makes audio formats play that encode the audio properties in
342 		// the audio container (e.g. WMA) and not in the audio frames
343 		// themself (e.g. MP3).
344 		// Note: Doing this step unconditionally is OK, because the first call
345 		// to _DecodeNextAudioFrameChunk() will update the essential audio
346 		// format properties accordingly regardless of the settings here.
347 
348 	// close any previous instance
349 	if (fCodecInitDone) {
350 		fCodecInitDone = false;
351 		avcodec_close(fContext);
352 	}
353 
354 	if (avcodec_open2(fContext, fCodec, NULL) >= 0)
355 		fCodecInitDone = true;
356 	else {
357 		TRACE("avcodec_open() failed to init codec!\n");
358 		return B_ERROR;
359 	}
360 
361 	free(fChunkBuffer);
362 	fChunkBuffer = NULL;
363 	fChunkBufferSize = 0;
364 	fAudioDecodeError = false;
365 	fDecodedDataBufferOffset = 0;
366 	fDecodedDataBufferSize = 0;
367 
368 	_ResetTempPacket();
369 
370 	status_t statusOfDecodingFirstFrameChunk = _DecodeNextAudioFrameChunk();
371 	if (statusOfDecodingFirstFrameChunk != B_OK) {
372 		TRACE("[a] decoding first audio frame chunk failed\n");
373 		return B_ERROR;
374 	}
375 
376 	media_multi_audio_format outputAudioFormat;
377 	outputAudioFormat = media_raw_audio_format::wildcard;
378 	outputAudioFormat.byte_order = B_MEDIA_HOST_ENDIAN;
379 	outputAudioFormat.frame_rate = fContext->sample_rate;
380 	outputAudioFormat.channel_count = fContext->channels;
381 	ConvertAVSampleFormatToRawAudioFormat(fContext->sample_fmt,
382 		outputAudioFormat.format);
383 	// Check that format is not still a wild card!
384 	if (outputAudioFormat.format == 0) {
385 		TRACE("  format still a wild-card, assuming B_AUDIO_SHORT.\n");
386 		outputAudioFormat.format = media_raw_audio_format::B_AUDIO_SHORT;
387 	}
388 	outputAudioFormat.buffer_size = inOutFormat->u.raw_audio.buffer_size;
389 	// Check that buffer_size has a sane value
390 	size_t sampleSize = outputAudioFormat.format
391 		& media_raw_audio_format::B_AUDIO_SIZE_MASK;
392 	if (outputAudioFormat.buffer_size == 0) {
393 		outputAudioFormat.buffer_size = 512 * sampleSize
394 			* outputAudioFormat.channel_count;
395 	}
396 
397 	inOutFormat->type = B_MEDIA_RAW_AUDIO;
398 	inOutFormat->u.raw_audio = outputAudioFormat;
399 	inOutFormat->require_flags = 0;
400 	inOutFormat->deny_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
401 
402 	// Initialize variables needed to manage decoding as much audio frames as
403 	// needed to fill the buffer_size.
404 	fOutputFrameSize = sampleSize * outputAudioFormat.channel_count;
405 	fOutputFrameCount = outputAudioFormat.buffer_size / fOutputFrameSize;
406 	fOutputFrameRate = outputAudioFormat.frame_rate;
407 	fRawDecodedAudio->opaque
408 		= av_realloc(fRawDecodedAudio->opaque, sizeof(avformat_codec_context));
409 	if (fRawDecodedAudio->opaque == NULL)
410 		return B_NO_MEMORY;
411 
412 	TRACE("  bit_rate = %d, sample_rate = %d, channels = %d, init = %d, "
413 		"output frame size: %d, count: %ld, rate: %.2f\n",
414 		fContext->bit_rate, fContext->sample_rate, fContext->channels,
415 		result, fOutputFrameSize, fOutputFrameCount, fOutputFrameRate);
416 
417 	return B_OK;
418 }
419 
420 
421 status_t
422 AVCodecDecoder::_NegotiateVideoOutputFormat(media_format* inOutFormat)
423 {
424 	TRACE("AVCodecDecoder::_NegotiateVideoOutputFormat()\n");
425 
426 	TRACE("  requested video format 0x%x\n",
427 		inOutFormat->u.raw_video.display.format);
428 
429 	_ApplyEssentialVideoContainerPropertiesToContext();
430 		// This makes video formats play that encode the video properties in
431 		// the video container (e.g. WMV) and not in the video frames
432 		// themself (e.g. MPEG2).
433 		// Note: Doing this step unconditionally is OK, because the first call
434 		// to _DecodeNextVideoFrame() will update the essential video format
435 		// properties accordingly regardless of the settings here.
436 
437 	bool codecCanHandleIncompleteFrames
438 		= (fCodec->capabilities & CODEC_CAP_TRUNCATED) != 0;
439 	if (codecCanHandleIncompleteFrames) {
440 		// Expect and handle video frames to be splitted across consecutive
441 		// data chunks.
442 		fContext->flags |= CODEC_FLAG_TRUNCATED;
443 	}
444 
445 	// close any previous instance
446 	if (fCodecInitDone) {
447 		fCodecInitDone = false;
448 		avcodec_close(fContext);
449 	}
450 
451 	if (avcodec_open2(fContext, fCodec, NULL) >= 0)
452 		fCodecInitDone = true;
453 	else {
454 		TRACE("avcodec_open() failed to init codec!\n");
455 		return B_ERROR;
456 	}
457 
458 	// Make MediaPlayer happy (if not in rgb32 screen depth and no overlay,
459 	// it will only ask for YCbCr, which DrawBitmap doesn't handle, so the
460 	// default colordepth is RGB32).
461 	if (inOutFormat->u.raw_video.display.format == B_YCbCr422)
462 		fOutputColorSpace = B_YCbCr422;
463 	else
464 		fOutputColorSpace = B_RGB32;
465 
466 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
467 	if (fSwsContext != NULL)
468 		sws_freeContext(fSwsContext);
469 	fSwsContext = NULL;
470 #else
471 	fFormatConversionFunc = 0;
472 #endif
473 
474 	free(fChunkBuffer);
475 	fChunkBuffer = NULL;
476 	fChunkBufferSize = 0;
477 
478 	_ResetTempPacket();
479 
480 	status_t statusOfDecodingFirstFrame = _DecodeNextVideoFrame();
481 	if (statusOfDecodingFirstFrame != B_OK) {
482 		TRACE("[v] decoding first video frame failed\n");
483 		return B_ERROR;
484 	}
485 
486 	// Note: fSwsContext / fFormatConversionFunc should have been initialized
487 	// by first call to _DecodeNextVideoFrame() above.
488 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
489 	if (fSwsContext == NULL) {
490 		TRACE("No SWS Scale context or decoder has not set the pixel format "
491 			"yet!\n");
492 	}
493 #else
494 	if (fFormatConversionFunc == NULL) {
495 		TRACE("no pixel format conversion function found or decoder has "
496 			"not set the pixel format yet!\n");
497 	}
498 #endif
499 
500 	inOutFormat->type = B_MEDIA_RAW_VIDEO;
501 	inOutFormat->require_flags = 0;
502 	inOutFormat->deny_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
503 	inOutFormat->u.raw_video = fInputFormat.u.encoded_video.output;
504 	inOutFormat->u.raw_video.interlace = 1;
505 		// Progressive (non-interlaced) video frames are delivered
506 	inOutFormat->u.raw_video.first_active = fHeader.u.raw_video.first_active_line;
507 	inOutFormat->u.raw_video.last_active = fHeader.u.raw_video.line_count;
508 	inOutFormat->u.raw_video.pixel_width_aspect = fHeader.u.raw_video.pixel_width_aspect;
509 	inOutFormat->u.raw_video.pixel_height_aspect = fHeader.u.raw_video.pixel_height_aspect;
510 	inOutFormat->u.raw_video.field_rate = fOutputFrameRate;
511 		// Was calculated by first call to _DecodeNextVideoFrame()
512 	inOutFormat->u.raw_video.display.format = fOutputColorSpace;
513 	inOutFormat->u.raw_video.display.line_width = fHeader.u.raw_video.display_line_width;
514 	inOutFormat->u.raw_video.display.line_count = fHeader.u.raw_video.display_line_count;
515 	inOutFormat->u.raw_video.display.bytes_per_row = fHeader.u.raw_video.bytes_per_row;
516 
517 #ifdef TRACE_AV_CODEC
518 	char buffer[1024];
519 	string_for_format(*inOutFormat, buffer, sizeof(buffer));
520 	TRACE("[v]  outFormat = %s\n", buffer);
521 	TRACE("  returned  video format 0x%x\n",
522 		inOutFormat->u.raw_video.display.format);
523 #endif
524 
525 	return B_OK;
526 }
527 
528 
529 /*! \brief Fills the outBuffer with one or more already decoded audio frames.
530 
531 	Besides the main duty described above, this method also fills out the other
532 	output parameters as documented below.
533 
534 	\param outBuffer Pointer to the output buffer to copy the decoded audio
535 		frames to.
536 	\param outFrameCount Pointer to the output variable to assign the number of
537 		copied audio frames (usually several audio frames at once).
538 	\param mediaHeader Pointer to the output media header that contains the
539 		properties of the decoded audio frame being the first in the outBuffer.
540 	\param info Specifies additional decoding parameters. (Note: unused).
541 
542 	\returns B_OK Decoding audio frames succeeded.
543 	\returns B_LAST_BUFFER_ERROR There are no more audio frames available.
544 	\returns Other error codes
545 */
546 status_t
547 AVCodecDecoder::_DecodeAudio(void* outBuffer, int64* outFrameCount,
548 	media_header* mediaHeader, media_decode_info* info)
549 {
550 	TRACE_AUDIO("AVCodecDecoder::_DecodeAudio(audio start_time %.6fs)\n",
551 		mediaHeader->start_time / 1000000.0);
552 
553 	status_t audioDecodingStatus
554 		= fDecodedDataSizeInBytes > 0 ? B_OK : _DecodeNextAudioFrame();
555 
556 	if (audioDecodingStatus != B_OK)
557 		return audioDecodingStatus;
558 
559 	*outFrameCount = fDecodedDataSizeInBytes / fOutputFrameSize;
560 	*mediaHeader = fHeader;
561 	memcpy(outBuffer, fDecodedData, fDecodedDataSizeInBytes);
562 
563 	fDecodedDataSizeInBytes = 0;
564 
565 	return B_OK;
566 }
567 
568 
569 /*! \brief Fills the outBuffer with an already decoded video frame.
570 
571 	Besides the main duty described above, this method also fills out the other
572 	output parameters as documented below.
573 
574 	\param outBuffer Pointer to the output buffer to copy the decoded video
575 		frame to.
576 	\param outFrameCount Pointer to the output variable to assign the number of
577 		copied video frames (usually one video frame).
578 	\param mediaHeader Pointer to the output media header that contains the
579 		decoded video frame properties.
580 	\param info Specifies additional decoding parameters. (Note: unused).
581 
582 	\returns B_OK Decoding a video frame succeeded.
583 	\returns B_LAST_BUFFER_ERROR There are no more video frames available.
584 	\returns Other error codes
585 */
586 status_t
587 AVCodecDecoder::_DecodeVideo(void* outBuffer, int64* outFrameCount,
588 	media_header* mediaHeader, media_decode_info* info)
589 {
590 	status_t videoDecodingStatus
591 		= fDecodedDataSizeInBytes > 0 ? B_OK : _DecodeNextVideoFrame();
592 
593 	if (videoDecodingStatus != B_OK)
594 		return videoDecodingStatus;
595 
596 	*outFrameCount = 1;
597 	*mediaHeader = fHeader;
598 	memcpy(outBuffer, fDecodedData, mediaHeader->size_used);
599 
600 	fDecodedDataSizeInBytes = 0;
601 
602 	return B_OK;
603 }
604 
605 
606 /*!	\brief Decodes next audio frame.
607 
608 	We decode at least one audio frame into fDecodedData. To achieve this goal,
609     we might need to request several chunks of encoded data resulting in a
610     variable execution time of this function.
611 
612     The length of the decoded audio frame(s) is stored in
613     fDecodedDataSizeInBytes. If this variable is greater than zero you can
614     assert that all audio frames in fDecodedData are valid.
615 
616 	It is assumed that the number of expected audio frames is stored in
617 	fOutputFrameCount. So _DecodeNextAudioFrame() must be called only after
618 	fOutputFrameCount has been set.
619 
620 	Note: fOutputFrameCount contains the maximum number of frames a caller
621 	of BMediaDecoder::Decode() expects to receive. There is a direct
622 	relationship between fOutputFrameCount and the buffer size a caller of
623 	BMediaDecoder::Decode() will provide so we make sure to respect this limit
624 	for fDecodedDataSizeInBytes.
625 
626 	On return with status code B_OK the following conditions hold true:
627 		1. fDecodedData contains as much audio frames as the caller of
628 		   BMediaDecoder::Decode() expects.
629 		2. fDecodedData contains lesser audio frames as the caller of
630 		   BMediaDecoder::Decode() expects only when one of the following
631 		   conditions hold true:
632 		       i  No more audio frames left. Consecutive calls to
633 		          _DecodeNextAudioFrame() will then result in the return of
634 		          status code B_LAST_BUFFER_ERROR.
635 		       ii TODO: A change in the size of the audio frames.
636 		3. fHeader is populated with the audio frame properties of the first
637 		   audio frame in fDecodedData. Especially the start_time field of
638 		   fHeader relates to that first audio frame. Start times of
639 		   consecutive audio frames in fDecodedData have to be calculated
640 		   manually (using the frame rate and the frame duration) if the
641 		   caller needs them.
642 
643 	TODO: Handle change of channel_count. Such a change results in a change of
644 	the audio frame size and thus has different buffer requirements.
645 	The most sane approach for implementing this is to return the audio frames
646 	that were still decoded with the previous channel_count and inform the
647 	client of BMediaDecoder::Decode() about the change so that it can adapt to
648 	it. Furthermore we need to adapt our fDecodedData to the new buffer size
649 	requirements accordingly.
650 
651 	\returns B_OK when we successfully decoded enough audio frames
652 	\returns B_LAST_BUFFER_ERROR when there are no more audio frames available.
653 	\returns Other Errors
654 */
655 status_t
656 AVCodecDecoder::_DecodeNextAudioFrame()
657 {
658 	assert(fTempPacket.size >= 0);
659 	assert(fDecodedDataSizeInBytes == 0);
660 		// _DecodeNextAudioFrame needs to be called on empty fDecodedData only!
661 		// If this assert holds wrong we have a bug somewhere.
662 
663 	status_t resetStatus = _ResetRawDecodedAudio();
664 	if (resetStatus != B_OK)
665 		return resetStatus;
666 
667 	while (fRawDecodedAudio->nb_samples < fOutputFrameCount) {
668 		_CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow();
669 
670 		bool decodedDataBufferHasData = fDecodedDataBufferSize > 0;
671 		if (decodedDataBufferHasData) {
672 			_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes();
673 			continue;
674 		}
675 
676 		status_t decodeAudioChunkStatus = _DecodeNextAudioFrameChunk();
677 		if (decodeAudioChunkStatus != B_OK)
678 			return decodeAudioChunkStatus;
679 	}
680 
681 	fFrame += fRawDecodedAudio->nb_samples;
682 	fDecodedDataSizeInBytes = fRawDecodedAudio->linesize[0];
683 
684 	_UpdateMediaHeaderForAudioFrame();
685 
686 #ifdef DEBUG
687 	dump_ffframe_audio(fRawDecodedAudio, "ffaudi");
688 #endif
689 
690 	TRACE_AUDIO("  frame count: %lld current: %lld\n",
691 		fRawDecodedAudio->nb_samples, fFrame);
692 
693 	return B_OK;
694 }
695 
696 
697 /*!	\brief Applies all essential audio input properties to fContext that were
698 		passed to AVCodecDecoder when Setup() was called.
699 
700 	Note: This function must be called before the AVCodec is opened via
701 	avcodec_open2(). Otherwise the behaviour of FFMPEG's audio decoding
702 	function avcodec_decode_audio4() is undefined.
703 
704 	Essential properties applied from fInputFormat.u.encoded_audio:
705 		- bit_rate copied to fContext->bit_rate
706 		- frame_size copied to fContext->frame_size
707 		- output.format converted to fContext->sample_fmt
708 		- output.frame_rate copied to fContext->sample_rate
709 		- output.channel_count copied to fContext->channels
710 
711 	Other essential properties being applied:
712 		- fBlockAlign to fContext->block_align
713 		- fExtraData to fContext->extradata
714 		- fExtraDataSize to fContext->extradata_size
715 
716 	TODO: Either the following documentation section should be removed or this
717 	TODO when it is clear whether fInputFormat.MetaData() and
718 	fInputFormat.MetaDataSize() have to be applied to fContext. See the related
719 	TODO in the method implementation.
720 	Only applied when fInputFormat.MetaDataSize() is greater than zero:
721 		- fInputFormat.MetaData() to fContext->extradata
722 		- fInputFormat.MetaDataSize() to fContext->extradata_size
723 */
724 void
725 AVCodecDecoder::_ApplyEssentialAudioContainerPropertiesToContext()
726 {
727 	media_encoded_audio_format containerProperties
728 		= fInputFormat.u.encoded_audio;
729 
730 	fContext->bit_rate
731 		= static_cast<int>(containerProperties.bit_rate);
732 	fContext->frame_size
733 		= static_cast<int>(containerProperties.frame_size);
734 	ConvertRawAudioFormatToAVSampleFormat(
735 		containerProperties.output.format, fContext->sample_fmt);
736 	fContext->sample_rate
737 		= static_cast<int>(containerProperties.output.frame_rate);
738 	fContext->channels
739 		= static_cast<int>(containerProperties.output.channel_count);
740 	// Check that channel count is not still a wild card!
741 	if (fContext->channels == 0) {
742 		TRACE("  channel_count still a wild-card, assuming stereo.\n");
743 		fContext->channels = 2;
744 	}
745 
746 	fContext->block_align = fBlockAlign;
747 	fContext->extradata = reinterpret_cast<uint8_t*>(fExtraData);
748 	fContext->extradata_size = fExtraDataSize;
749 
750 	// TODO: This probably needs to go away, there is some misconception
751 	// about extra data / info buffer and meta data. See
752 	// Reader::GetStreamInfo(). The AVFormatReader puts extradata and
753 	// extradata_size into media_format::MetaData(), but used to ignore
754 	// the infoBuffer passed to GetStreamInfo(). I think this may be why
755 	// the code below was added.
756 	if (fInputFormat.MetaDataSize() > 0) {
757 		fContext->extradata = static_cast<uint8_t*>(
758 			const_cast<void*>(fInputFormat.MetaData()));
759 		fContext->extradata_size = fInputFormat.MetaDataSize();
760 	}
761 
762 	TRACE("  bit_rate %d, sample_rate %d, channels %d, block_align %d, "
763 		"extradata_size %d\n", fContext->bit_rate, fContext->sample_rate,
764 		fContext->channels, fContext->block_align, fContext->extradata_size);
765 }
766 
767 
768 /*!	\brief Resets important fields in fRawDecodedVideo to their default values.
769 
770 	Note: Also initializes fDecodedData if not done already.
771 
772 	\returns B_OK Resetting successfully completed.
773 	\returns B_NO_MEMORY No memory left for correct operation.
774 */
775 status_t
776 AVCodecDecoder::_ResetRawDecodedAudio()
777 {
778 	if (fDecodedData == NULL) {
779 		size_t maximumSizeOfDecodedData = fOutputFrameCount * fOutputFrameSize;
780 		fDecodedData
781 			= static_cast<uint8_t*>(malloc(maximumSizeOfDecodedData));
782 	}
783 	if (fDecodedData == NULL)
784 		return B_NO_MEMORY;
785 
786 	fRawDecodedAudio->data[0] = fDecodedData;
787 	fRawDecodedAudio->linesize[0] = 0;
788 	fRawDecodedAudio->format = AV_SAMPLE_FMT_NONE;
789 	fRawDecodedAudio->pkt_dts = AV_NOPTS_VALUE;
790 	fRawDecodedAudio->nb_samples = 0;
791 	memset(fRawDecodedAudio->opaque, 0, sizeof(avformat_codec_context));
792 
793 	return B_OK;
794 }
795 
796 
797 /*!	\brief Checks fDecodedDataBufferSize and fTempPacket for invalid values,
798 		reports them and assigns valid values.
799 
800 	Note: This method is intended to be called before any code is executed that
801 	deals with moving, loading or decoding any audio frames.
802 */
803 void
804 AVCodecDecoder::_CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow()
805 {
806 	if (fDecodedDataBufferSize < 0) {
807 		fprintf(stderr, "Decoding read past the end of the decoded data "
808 			"buffer! %ld\n", fDecodedDataBufferSize);
809 		fDecodedDataBufferSize = 0;
810 	}
811 	if (fTempPacket.size < 0) {
812 		fprintf(stderr, "Decoding read past the end of the temp packet! %d\n",
813 			fTempPacket.size);
814 		fTempPacket.size = 0;
815 	}
816 }
817 
818 
819 /*!	\brief Moves audio frames from fDecodedDataBuffer to fRawDecodedAudio (and
820 		thus to fDecodedData) and updates the start times of fRawDecodedAudio,
821 		fDecodedDataBuffer and fTempPacket accordingly.
822 
823 	When moving audio frames to fRawDecodedAudio this method also makes sure
824 	that the following important fields of fRawDecodedAudio are populated and
825 	updated with correct values:
826 		- fRawDecodedAudio->data[0]: Points to first free byte of fDecodedData
827 		- fRawDecodedAudio->linesize[0]: Total size of frames in fDecodedData
828 		- fRawDecodedAudio->format: Format of first audio frame
829 		- fRawDecodedAudio->pkt_dts: Start time of first audio frame
830 		- fRawDecodedAudio->nb_samples: Number of audio frames
831 		- fRawDecodedAudio->opaque: Contains the following fields for the first
832 		  audio frame:
833 		      - channels: Channel count of first audio frame
834 		      - sample_rate: Frame rate of first audio frame
835 
836 	This function assumes to be called only when the following assumptions
837 	hold true:
838 		1. There are decoded audio frames available in fDecodedDataBuffer
839 		   meaning that fDecodedDataBufferSize is greater than zero.
840 		2. There is space left in fRawDecodedAudio to move some audio frames
841 		   in. This means that fRawDecodedAudio has lesser audio frames than
842 		   the maximum allowed (specified by fOutputFrameCount).
843 		3. The audio frame rate is known so that we can calculate the time
844 		   range (covered by the moved audio frames) to update the start times
845 		   accordingly.
846 		4. The field fRawDecodedAudio->opaque points to a memory block
847 		   representing a structure of type avformat_codec_context.
848 
849 	After this function returns the caller can safely make the following
850 	assumptions:
851 		1. The number of decoded audio frames in fDecodedDataBuffer is
852 		   decreased though it may still be greater then zero.
853 		2. The number of frames in fRawDecodedAudio has increased and all
854 		   important fields are updated (see listing above).
855 		3. Start times of fDecodedDataBuffer and fTempPacket were increased
856 		   with the time range covered by the moved audio frames.
857 
858 	Note: This function raises an exception (by calling the debugger), when
859 	fDecodedDataBufferSize is not a multiple of fOutputFrameSize.
860 */
861 void
862 AVCodecDecoder::_MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes()
863 {
864 	assert(fDecodedDataBufferSize > 0);
865 	assert(fRawDecodedAudio->nb_samples < fOutputFrameCount);
866 	assert(fOutputFrameRate > 0);
867 
868 	int32 frames = min_c(fOutputFrameCount - fRawDecodedAudio->nb_samples,
869 		fDecodedDataBufferSize / fOutputFrameSize);
870 	if (frames == 0)
871 		debugger("fDecodedDataBufferSize not multiple of frame size!");
872 
873 	size_t remainingSize = frames * fOutputFrameSize;
874 	memcpy(fRawDecodedAudio->data[0], fDecodedDataBuffer->data[0]
875 		+ fDecodedDataBufferOffset, remainingSize);
876 
877 	bool firstAudioFramesCopiedToRawDecodedAudio
878 		= fRawDecodedAudio->data[0] != fDecodedData;
879 	if (!firstAudioFramesCopiedToRawDecodedAudio) {
880 		fRawDecodedAudio->format = fDecodedDataBuffer->format;
881 		fRawDecodedAudio->pkt_dts = fDecodedDataBuffer->pkt_dts;
882 
883 		avformat_codec_context* codecContext
884 			= static_cast<avformat_codec_context*>(fRawDecodedAudio->opaque);
885 		codecContext->channels = fContext->channels;
886 		codecContext->sample_rate = fContext->sample_rate;
887 	}
888 
889 	fRawDecodedAudio->data[0] += remainingSize;
890 	fRawDecodedAudio->linesize[0] += remainingSize;
891 	fRawDecodedAudio->nb_samples += frames;
892 
893 	fDecodedDataBufferOffset += remainingSize;
894 	fDecodedDataBufferSize -= remainingSize;
895 
896 	// Update start times accordingly
897 	bigtime_t framesTimeInterval = static_cast<bigtime_t>(
898 		(1000000LL * frames) / fOutputFrameRate);
899 	fDecodedDataBuffer->pkt_dts += framesTimeInterval;
900 		// Start time of buffer is updated in case that it contains
901 		// more audio frames to move.
902 	fTempPacket.dts += framesTimeInterval;
903 		// Start time of fTempPacket is updated in case the fTempPacket
904 		// contains more audio frames to decode.
905 }
906 
907 
908 /*!	\brief Decodes next chunk of audio frames.
909 
910 	This method handles all the details of loading the input buffer
911 	(fChunkBuffer) at the right time and of calling FFMPEG often engouh until
912 	some audio frames have been decoded.
913 
914 	FFMPEG decides how much audio frames belong to a chunk. Because of that
915 	it is very likely that _DecodeNextAudioFrameChunk has to be called several
916 	times to decode enough audio frames to please the caller of
917 	BMediaDecoder::Decode().
918 
919 	This function assumes to be called only when the following assumptions
920 	hold true:
921 		1. fDecodedDataBufferSize equals zero.
922 
923 	After this function returns successfully the caller can safely make the
924 	following assumptions:
925 		1. fDecodedDataBufferSize is greater than zero.
926 		2. fDecodedDataBufferOffset is set to zero.
927 		3. fDecodedDataBuffer contains audio frames.
928 
929 	\returns B_OK on successfully decoding one audio frame chunk.
930 	\returns B_LAST_BUFFER_ERROR No more audio frame chunks available. From
931 		this point on further calls will return this same error.
932 	\returns B_ERROR Decoding failed
933 */
934 status_t
935 AVCodecDecoder::_DecodeNextAudioFrameChunk()
936 {
937 	assert(fDecodedDataBufferSize == 0);
938 
939 	while(fDecodedDataBufferSize == 0) {
940 		status_t loadingChunkStatus
941 			= _LoadNextChunkIfNeededAndAssignStartTime();
942 		if (loadingChunkStatus != B_OK)
943 			return loadingChunkStatus;
944 
945 		status_t decodingStatus
946 			= _DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer();
947 		if (decodingStatus != B_OK) {
948 			// Assume the audio decoded until now is broken so replace it with
949 			// some silence.
950 			memset(fDecodedData, 0, fRawDecodedAudio->linesize[0]);
951 
952 			if (!fAudioDecodeError) {
953 				// Report failure if not done already
954 				int32 chunkBufferOffset = fTempPacket.data - fChunkBuffer;
955 				printf("########### audio decode error, "
956 					"fTempPacket.size %d, fChunkBuffer data offset %ld\n",
957 					fTempPacket.size, chunkBufferOffset);
958 				fAudioDecodeError = true;
959 			}
960 
961 			// Assume that next audio chunk can be decoded so keep decoding.
962 			continue;
963 		}
964 
965 		fAudioDecodeError = false;
966 	}
967 
968 	return B_OK;
969 }
970 
971 
972 /*!	\brief Tries to decode at least one audio frame and store it in the
973 		fDecodedDataBuffer.
974 
975 	This function assumes to be called only when the following assumptions
976 	hold true:
977 		1. fDecodedDataBufferSize equals zero.
978 		2. fTempPacket.size is greater than zero.
979 
980 	After this function returns successfully the caller can safely make the
981 	following assumptions:
982 		1. fDecodedDataBufferSize is greater than zero in the common case.
983 		   Also see "Note" below.
984 		2. fTempPacket was updated to exclude the data chunk that was consumed
985 		   by avcodec_decode_audio4().
986 		3. fDecodedDataBufferOffset is set to zero.
987 
988 	When this function failed to decode at least one audio frame due to a
989 	decoding error the caller can safely make the following assumptions:
990 		1. fDecodedDataBufferSize equals zero.
991 		2. fTempPacket.size equals zero.
992 
993 	Note: It is possible that there wasn't any audio frame decoded into
994 	fDecodedDataBuffer after calling this function. This is normal and can
995 	happen when there was either a decoding error or there is some decoding
996 	delay in FFMPEGs audio decoder. Another call to this method is totally
997 	safe and is even expected as long as the calling assumptions hold true.
998 
999 	\returns B_OK Decoding successful. fDecodedDataBuffer contains decoded
1000 		audio frames only when fDecodedDataBufferSize is greater than zero.
1001 		fDecodedDataBuffer is empty, when avcodec_decode_audio4() didn't return
1002 		audio frames due to delayed decoding or incomplete audio frames.
1003 	\returns B_ERROR Decoding failed thus fDecodedDataBuffer contains no audio
1004 		frames.
1005 */
1006 status_t
1007 AVCodecDecoder::_DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer()
1008 {
1009 	assert(fDecodedDataBufferSize == 0);
1010 	assert(fTempPacket.size > 0);
1011 
1012 	avcodec_get_frame_defaults(fDecodedDataBuffer);
1013 	fDecodedDataBufferOffset = 0;
1014 	int gotAudioFrame = 0;
1015 
1016 	int encodedDataSizeInBytes = avcodec_decode_audio4(fContext,
1017 		fDecodedDataBuffer, &gotAudioFrame, &fTempPacket);
1018 	if (encodedDataSizeInBytes <= 0) {
1019 		// Error or failure to produce decompressed output.
1020 		// Skip the temp packet data entirely.
1021 		fTempPacket.size = 0;
1022 		return B_ERROR;
1023 	}
1024 
1025 	fTempPacket.data += encodedDataSizeInBytes;
1026 	fTempPacket.size -= encodedDataSizeInBytes;
1027 
1028 	bool gotNoAudioFrame = gotAudioFrame == 0;
1029 	if (gotNoAudioFrame)
1030 		return B_OK;
1031 
1032 	fDecodedDataBufferSize = av_samples_get_buffer_size(NULL,
1033 		fContext->channels, fDecodedDataBuffer->nb_samples,
1034 		fContext->sample_fmt, 1);
1035 	if (fDecodedDataBufferSize < 0)
1036 		fDecodedDataBufferSize = 0;
1037 
1038 	return B_OK;
1039 }
1040 
1041 
1042 /*! \brief Updates relevant fields of the class member fHeader with the
1043 		properties of the most recently decoded audio frame.
1044 
1045 	The following fields of fHeader are updated:
1046 		- fHeader.type
1047 		- fHeader.file_pos
1048 		- fHeader.orig_size
1049 		- fHeader.start_time
1050 		- fHeader.size_used
1051 		- fHeader.u.raw_audio.frame_rate
1052 		- fHeader.u.raw_audio.channel_count
1053 
1054 	It is assumed that this function is called only	when the following asserts
1055 	hold true:
1056 		1. We actually got a new audio frame decoded by the audio decoder.
1057 		2. fHeader wasn't updated for the new audio frame yet. You MUST call
1058 		   this method only once per decoded audio frame.
1059 		3. fRawDecodedAudio's fields relate to the first audio frame contained
1060 		   in fDecodedData. Especially the following fields are of importance:
1061 		       - fRawDecodedAudio->pkt_dts: Start time of first audio frame
1062 		       - fRawDecodedAudio->opaque: Contains the following fields for
1063 		         the first audio frame:
1064 			         - channels: Channel count of first audio frame
1065 			         - sample_rate: Frame rate of first audio frame
1066 */
1067 void
1068 AVCodecDecoder::_UpdateMediaHeaderForAudioFrame()
1069 {
1070 	fHeader.type = B_MEDIA_RAW_AUDIO;
1071 	fHeader.file_pos = 0;
1072 	fHeader.orig_size = 0;
1073 	fHeader.start_time = fRawDecodedAudio->pkt_dts;
1074 	fHeader.size_used = fRawDecodedAudio->linesize[0];
1075 
1076 	avformat_codec_context* codecContext
1077 		= static_cast<avformat_codec_context*>(fRawDecodedAudio->opaque);
1078 	fHeader.u.raw_audio.channel_count = codecContext->channels;
1079 	fHeader.u.raw_audio.frame_rate = codecContext->sample_rate;
1080 }
1081 
1082 
1083 /*! \brief Decodes next video frame.
1084 
1085     We decode exactly one video frame into fDecodedData. To achieve this goal,
1086     we might need to request several chunks of encoded data resulting in a
1087     variable execution time of this function.
1088 
1089     The length of the decoded video frame is stored in
1090     fDecodedDataSizeInBytes. If this variable is greater than zero, you can
1091     assert that there is a valid video frame available in fDecodedData.
1092 
1093     The decoded video frame in fDecodedData has color space conversion and
1094     deinterlacing already applied.
1095 
1096     To every decoded video frame there is a media_header populated in
1097     fHeader, containing the corresponding video frame properties.
1098 
1099 	Normally every decoded video frame has a start_time field populated in the
1100 	associated fHeader, that determines the presentation time of the frame.
1101 	This relationship will only hold true, when each data chunk that is
1102 	provided via GetNextChunk() contains data for exactly one encoded video
1103 	frame (one complete frame) - not more and not less.
1104 
1105 	We can decode data chunks that contain partial video frame data, too. In
1106 	that case, you cannot trust the value of the start_time field in fHeader.
1107 	We simply have no logic in place to establish a meaningful relationship
1108 	between an incomplete frame and the start time it should be presented.
1109 	Though this	might change in the future.
1110 
1111 	We can decode data chunks that contain more than one video frame, too. In
1112 	that case, you cannot trust the value of the start_time field in fHeader.
1113 	We simply have no logic in place to track the start_time across multiple
1114 	video frames. So a meaningful relationship between the 2nd, 3rd, ... frame
1115 	and the start time it should be presented isn't established at the moment.
1116 	Though this	might change in the future.
1117 
1118     More over the fOutputFrameRate variable is updated for every decoded video
1119     frame.
1120 
1121 	On first call the member variables fSwsContext / fFormatConversionFunc	are
1122 	initialized.
1123 
1124 	\returns B_OK when we successfully decoded one video frame
1125 	\returns B_LAST_BUFFER_ERROR when there are no more video frames available.
1126 	\returns B_NO_MEMORY when we have no memory left for correct operation.
1127 	\returns Other Errors
1128  */
1129 status_t
1130 AVCodecDecoder::_DecodeNextVideoFrame()
1131 {
1132 	assert(fTempPacket.size >= 0);
1133 
1134 	while (true) {
1135 		status_t loadingChunkStatus
1136 			= _LoadNextChunkIfNeededAndAssignStartTime();
1137 		if (loadingChunkStatus == B_LAST_BUFFER_ERROR)
1138 			return _FlushOneVideoFrameFromDecoderBuffer();
1139 		if (loadingChunkStatus != B_OK) {
1140 			TRACE("AVCodecDecoder::_DecodeNextVideoFrame(): error from "
1141 				"GetNextChunk(): %s\n", strerror(loadingChunkStatus));
1142 			return loadingChunkStatus;
1143 		}
1144 
1145 #if DO_PROFILING
1146 		bigtime_t startTime = system_time();
1147 #endif
1148 
1149 		// NOTE: In the FFMPEG 0.10.2 code example decoding_encoding.c, the
1150 		// length returned by avcodec_decode_video2() is used to update the
1151 		// packet buffer size (here it is fTempPacket.size). This way the
1152 		// packet buffer is allowed to contain incomplete frames so we are
1153 		// required to buffer the packets between different calls to
1154 		// _DecodeNextVideoFrame().
1155 		int gotVideoFrame = 0;
1156 		int encodedDataSizeInBytes = avcodec_decode_video2(fContext,
1157 			fRawDecodedPicture, &gotVideoFrame, &fTempPacket);
1158 		if (encodedDataSizeInBytes < 0) {
1159 			TRACE("[v] AVCodecDecoder: ignoring error in decoding frame %lld:"
1160 				" %d\n", fFrame, len);
1161 			// NOTE: An error from avcodec_decode_video2() is ignored by the
1162 			// FFMPEG 0.10.2 example decoding_encoding.c. Only the packet
1163 			// buffers are flushed accordingly
1164 			fTempPacket.data = NULL;
1165 			fTempPacket.size = 0;
1166 			continue;
1167 		}
1168 
1169 		fTempPacket.size -= encodedDataSizeInBytes;
1170 		fTempPacket.data += encodedDataSizeInBytes;
1171 
1172 		bool gotNoVideoFrame = gotVideoFrame == 0;
1173 		if (gotNoVideoFrame) {
1174 			TRACE("frame %lld - no picture yet, encodedDataSizeInBytes: %d, "
1175 				"chunk size: %ld\n", fFrame, encodedDataSizeInBytes,
1176 				fChunkBufferSize);
1177 			continue;
1178 		}
1179 
1180 #if DO_PROFILING
1181 		bigtime_t formatConversionStart = system_time();
1182 #endif
1183 
1184 		status_t handleStatus = _HandleNewVideoFrameAndUpdateSystemState();
1185 		if (handleStatus != B_OK)
1186 			return handleStatus;
1187 
1188 #if DO_PROFILING
1189 		bigtime_t doneTime = system_time();
1190 		decodingTime += formatConversionStart - startTime;
1191 		conversionTime += doneTime - formatConversionStart;
1192 		profileCounter++;
1193 		if (!(fFrame % 5)) {
1194 			printf("[v] profile: d1 = %lld, d2 = %lld (%lld) required %Ld\n",
1195 				decodingTime / profileCounter, conversionTime / profileCounter,
1196 				fFrame, bigtime_t(1000000LL / fOutputFrameRate));
1197 			decodingTime = 0;
1198 			conversionTime = 0;
1199 			profileCounter = 0;
1200 		}
1201 #endif
1202 		return B_OK;
1203 	}
1204 }
1205 
1206 
1207 /*!	\brief Applies all essential video input properties to fContext that were
1208 		passed to AVCodecDecoder when Setup() was called.
1209 
1210 	Note: This function must be called before the AVCodec is opened via
1211 	avcodec_open2(). Otherwise the behaviour of FFMPEG's video decoding
1212 	function avcodec_decode_video2() is undefined.
1213 
1214 	Essential properties applied from fInputFormat.u.encoded_video.output:
1215 		- display.line_width copied to fContext->width
1216 		- display.line_count copied to fContext->height
1217 		- pixel_width_aspect and pixel_height_aspect converted to
1218 		  fContext->sample_aspect_ratio
1219 		- field_rate converted to fContext->time_base and
1220 		  fContext->ticks_per_frame
1221 
1222 	Other essential properties being applied:
1223 		- fExtraData to fContext->extradata
1224 		- fExtraDataSize to fContext->extradata_size
1225 */
1226 void
1227 AVCodecDecoder::_ApplyEssentialVideoContainerPropertiesToContext()
1228 {
1229 	media_raw_video_format containerProperties
1230 		= fInputFormat.u.encoded_video.output;
1231 
1232 	fContext->width = containerProperties.display.line_width;
1233 	fContext->height = containerProperties.display.line_count;
1234 
1235 	if (containerProperties.pixel_width_aspect > 0
1236 		&& containerProperties.pixel_height_aspect > 0) {
1237 		ConvertVideoAspectWidthAndHeightToAVCodecContext(
1238 			containerProperties.pixel_width_aspect,
1239 			containerProperties.pixel_height_aspect, *fContext);
1240 	}
1241 
1242 	if (containerProperties.field_rate > 0.0) {
1243 		ConvertVideoFrameRateToAVCodecContext(containerProperties.field_rate,
1244 			*fContext);
1245 	}
1246 
1247 	fContext->extradata = reinterpret_cast<uint8_t*>(fExtraData);
1248 	fContext->extradata_size = fExtraDataSize;
1249 }
1250 
1251 
1252 /*! \brief Loads the next  chunk into fChunkBuffer and assigns it (including
1253 		the start time) to fTempPacket but only if fTempPacket is empty.
1254 
1255 	\returns B_OK
1256 		1. meaning: Next chunk is loaded.
1257 		2. meaning: No need to load and assign anything. Proceed as usual.
1258 	\returns B_LAST_BUFFER_ERROR No more chunks available. fChunkBuffer	and
1259 		fTempPacket are left untouched.
1260 	\returns Other errors Caller should bail out because fChunkBuffer and
1261 		fTempPacket are in unknown states. Normal operation cannot be
1262 		guaranteed.
1263 */
1264 status_t
1265 AVCodecDecoder::_LoadNextChunkIfNeededAndAssignStartTime()
1266 {
1267 	if (fTempPacket.size > 0)
1268 		return B_OK;
1269 
1270 	const void* chunkBuffer = NULL;
1271 	size_t chunkBufferSize = 0;
1272 		// In the case that GetNextChunk() returns an error fChunkBufferSize
1273 		// should be left untouched.
1274 	media_header chunkMediaHeader;
1275 
1276 	status_t getNextChunkStatus = GetNextChunk(&chunkBuffer, &chunkBufferSize,
1277 		&chunkMediaHeader);
1278 	if (getNextChunkStatus != B_OK)
1279 		return getNextChunkStatus;
1280 
1281 	status_t chunkBufferPaddingStatus
1282 		= _CopyChunkToChunkBufferAndAddPadding(chunkBuffer, chunkBufferSize);
1283 	if (chunkBufferPaddingStatus != B_OK)
1284 		return chunkBufferPaddingStatus;
1285 
1286 	fTempPacket.data = fChunkBuffer;
1287 	fTempPacket.size = fChunkBufferSize;
1288 	fTempPacket.dts = chunkMediaHeader.start_time;
1289 		// Let FFMPEG handle the correct relationship between start_time and
1290 		// decoded a/v frame. By doing so we are simply copying the way how it
1291 		// is implemented in ffplay.c for video frames (for audio frames it
1292 		// works, too, but isn't used by ffplay.c).
1293 		// \see http://git.videolan.org/?p=ffmpeg.git;a=blob;f=ffplay.c;h=09623db374e5289ed20b7cc28c262c4375a8b2e4;hb=9153b33a742c4e2a85ff6230aea0e75f5a8b26c2#l1502
1294 		//
1295 		// FIXME: Research how to establish a meaningful relationship between
1296 		// start_time and decoded a/v frame when the received chunk buffer
1297 		// contains partial a/v frames. Maybe some data formats do contain time
1298 		// stamps (ake pts / dts fields) that can be evaluated by FFMPEG. But
1299 		// as long as I don't have such video data to test it, it makes no
1300 		// sense trying to implement it.
1301 		//
1302 		// FIXME: Implement tracking start_time of video frames originating in
1303 		// data chunks that encode more than one video frame at a time. In that
1304 		// case on would increment the start_time for each consecutive frame of
1305 		// such a data chunk (like it is done for audio frame decoding). But as
1306 		// long as I don't have such video data to test it, it makes no sense
1307 		// to implement it.
1308 
1309 #ifdef LOG_STREAM_TO_FILE
1310 	BFile* logFile = fIsAudio ? &sAudioStreamLogFile : &sVideoStreamLogFile;
1311 	if (sDumpedPackets < 100) {
1312 		logFile->Write(chunkBuffer, fChunkBufferSize);
1313 		printf("wrote %ld bytes\n", fChunkBufferSize);
1314 		sDumpedPackets++;
1315 	} else if (sDumpedPackets == 100)
1316 		logFile->Unset();
1317 #endif
1318 
1319 	return B_OK;
1320 }
1321 
1322 
1323 /*! \brief Copies a chunk into fChunkBuffer and adds a "safety net" of
1324 		additional memory as required by FFMPEG for input buffers to video
1325 		decoders.
1326 
1327 	This is needed so that some decoders can read safely a predefined number of
1328 	bytes at a time for performance optimization purposes.
1329 
1330 	The additional memory has a size of FF_INPUT_BUFFER_PADDING_SIZE as defined
1331 	in avcodec.h.
1332 
1333 	Ownership of fChunkBuffer memory is with the class so it needs to be freed
1334 	at the right times (on destruction, on seeking).
1335 
1336 	Also update fChunkBufferSize to reflect the size of the contained data
1337 	(leaving out the padding).
1338 
1339 	\param chunk The chunk to copy.
1340 	\param chunkSize Size of the chunk in bytes
1341 
1342 	\returns B_OK Padding was successful. You are responsible for releasing the
1343 		allocated memory. fChunkBufferSize is set to chunkSize.
1344 	\returns B_NO_MEMORY Padding failed.
1345 		fChunkBuffer is set to NULL making it safe to call free() on it.
1346 		fChunkBufferSize is set to 0 to reflect the size of fChunkBuffer.
1347 */
1348 status_t
1349 AVCodecDecoder::_CopyChunkToChunkBufferAndAddPadding(const void* chunk,
1350 	size_t chunkSize)
1351 {
1352 	fChunkBuffer = static_cast<uint8_t*>(realloc(fChunkBuffer,
1353 		chunkSize + FF_INPUT_BUFFER_PADDING_SIZE));
1354 	if (fChunkBuffer == NULL) {
1355 		fChunkBufferSize = 0;
1356 		return B_NO_MEMORY;
1357 	}
1358 
1359 	memcpy(fChunkBuffer, chunk, chunkSize);
1360 	memset(fChunkBuffer + chunkSize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1361 		// Establish safety net, by zero'ing the padding area.
1362 
1363 	fChunkBufferSize = chunkSize;
1364 
1365 	return B_OK;
1366 }
1367 
1368 
1369 /*! \brief Executes all steps needed for a freshly decoded video frame.
1370 
1371 	\see _UpdateMediaHeaderForVideoFrame() and
1372 	\see _DeinterlaceAndColorConvertVideoFrame() for when you are allowed to
1373 	call this method.
1374 
1375 	\returns B_OK when video frame was handled successfully
1376 	\returnb B_NO_MEMORY when no memory is left for correct operation.
1377 */
1378 status_t
1379 AVCodecDecoder::_HandleNewVideoFrameAndUpdateSystemState()
1380 {
1381 	_UpdateMediaHeaderForVideoFrame();
1382 	status_t postProcessStatus = _DeinterlaceAndColorConvertVideoFrame();
1383 	if (postProcessStatus != B_OK)
1384 		return postProcessStatus;
1385 
1386 	ConvertAVCodecContextToVideoFrameRate(*fContext, fOutputFrameRate);
1387 
1388 #ifdef DEBUG
1389 	dump_ffframe_video(fRawDecodedPicture, "ffpict");
1390 #endif
1391 
1392 	fFrame++;
1393 
1394 	return B_OK;
1395 }
1396 
1397 
1398 /*! \brief Flushes one video frame - if any - still buffered by the decoder.
1399 
1400 	Some FFMPEG decoder are buffering video frames. To retrieve those buffered
1401 	frames the decoder needs to be told so.
1402 
1403 	The intended use of this method is to call it, once there are no more data
1404 	chunks for decoding left. Reframed in other words: Once GetNextChunk()
1405 	returns with status B_LAST_BUFFER_ERROR it is time to start flushing.
1406 
1407 	\returns B_OK Retrieved one video frame, handled it accordingly and updated
1408 		the system state accordingly.
1409 		There maybe more video frames left. So it is valid for the client of
1410 		AVCodecDecoder to call it one more time.
1411 
1412 	\returns B_LAST_BUFFER_ERROR No video frame left.
1413 		The client of the AVCodecDecoder should stop calling it now.
1414 
1415 	\returns B_NO_MEMORY No memory left for correct operation.
1416 */
1417 status_t
1418 AVCodecDecoder::_FlushOneVideoFrameFromDecoderBuffer()
1419 {
1420 	// Create empty fTempPacket to tell the video decoder it is time to flush
1421 	fTempPacket.data = NULL;
1422 	fTempPacket.size = 0;
1423 
1424 	int gotVideoFrame = 0;
1425 	avcodec_decode_video2(fContext,	fRawDecodedPicture, &gotVideoFrame,
1426 		&fTempPacket);
1427 		// We are only interested in complete frames now, so ignore the return
1428 		// value.
1429 
1430 	bool gotNoVideoFrame = gotVideoFrame == 0;
1431 	if (gotNoVideoFrame) {
1432 		// video buffer is flushed successfully
1433 		return B_LAST_BUFFER_ERROR;
1434 	}
1435 
1436 	return _HandleNewVideoFrameAndUpdateSystemState();
1437 }
1438 
1439 
1440 /*! \brief Updates relevant fields of the class member fHeader with the
1441 		properties of the most recently decoded video frame.
1442 
1443 	It is assumed that this function is called only	when the following asserts
1444 	hold true:
1445 		1. We actually got a new picture decoded by the video decoder.
1446 		2. fHeader wasn't updated for the new picture yet. You MUST call this
1447 		   method only once per decoded video frame.
1448 		3. This function MUST be called after
1449 		   _DeinterlaceAndColorConvertVideoFrame() as it relys on an updated
1450 		    fDecodedDataSizeInBytes.
1451 		4. There will be at maximumn only one decoded video frame in our cache
1452 		   at any single point in time. Otherwise you couldn't tell to which
1453 		   cached decoded video frame the properties in fHeader relate to.
1454 		5. AVCodecContext is still valid for this video frame (This is the case
1455 		   when this function is called after avcodec_decode_video2() and
1456 		   before the next call to avcodec_decode_video2().
1457 */
1458 void
1459 AVCodecDecoder::_UpdateMediaHeaderForVideoFrame()
1460 {
1461 	fHeader.type = B_MEDIA_RAW_VIDEO;
1462 	fHeader.file_pos = 0;
1463 	fHeader.orig_size = 0;
1464 	fHeader.start_time = fRawDecodedPicture->pkt_dts;
1465 	fHeader.size_used = avpicture_get_size(
1466 		colorspace_to_pixfmt(fOutputColorSpace), fRawDecodedPicture->width,
1467 		fRawDecodedPicture->height);
1468 	fHeader.u.raw_video.display_line_width = fRawDecodedPicture->width;
1469 	fHeader.u.raw_video.display_line_count = fRawDecodedPicture->height;
1470 	fHeader.u.raw_video.bytes_per_row
1471 		= CalculateBytesPerRowWithColorSpaceAndVideoWidth(fOutputColorSpace,
1472 			fRawDecodedPicture->width);
1473 	fHeader.u.raw_video.field_gamma = 1.0;
1474 	fHeader.u.raw_video.field_sequence = fFrame;
1475 	fHeader.u.raw_video.field_number = 0;
1476 	fHeader.u.raw_video.pulldown_number = 0;
1477 	fHeader.u.raw_video.first_active_line = 1;
1478 	fHeader.u.raw_video.line_count = fRawDecodedPicture->height;
1479 
1480 	ConvertAVCodecContextToVideoAspectWidthAndHeight(*fContext,
1481 		fHeader.u.raw_video.pixel_width_aspect,
1482 		fHeader.u.raw_video.pixel_height_aspect);
1483 
1484 	TRACE("[v] start_time=%02d:%02d.%02d field_sequence=%lu\n",
1485 		int((fHeader.start_time / 60000000) % 60),
1486 		int((fHeader.start_time / 1000000) % 60),
1487 		int((fHeader.start_time / 10000) % 100),
1488 		fHeader.u.raw_video.field_sequence);
1489 }
1490 
1491 
1492 /*! \brief This function applies deinterlacing (only if needed) and color
1493 	conversion to the video frame in fRawDecodedPicture.
1494 
1495 	It is assumed that fRawDecodedPicture wasn't deinterlaced and color
1496 	converted yet (otherwise this function behaves in unknown manners).
1497 
1498 	This function MUST be called after _UpdateMediaHeaderForVideoFrame() as it
1499 	relys on the fHeader.size_used and fHeader.u.raw_video.bytes_per_row fields
1500 	for correct operation
1501 
1502 	You should only call this function when you	got a new picture decoded by
1503 	the video decoder.
1504 
1505 	When this function finishes the postprocessed video frame will be available
1506 	in fPostProcessedDecodedPicture and fDecodedData (fDecodedDataSizeInBytes
1507 	will be set accordingly).
1508 
1509 	\returns B_OK video frame successfully deinterlaced and color converted.
1510 	\returns B_NO_MEMORY Not enough memory available for correct operation.
1511 */
1512 status_t
1513 AVCodecDecoder::_DeinterlaceAndColorConvertVideoFrame()
1514 {
1515 	int displayWidth = fRawDecodedPicture->width;
1516 	int displayHeight = fRawDecodedPicture->height;
1517 	AVPicture deinterlacedPicture;
1518 	bool useDeinterlacedPicture = false;
1519 
1520 	if (fRawDecodedPicture->interlaced_frame) {
1521 		AVPicture rawPicture;
1522 		rawPicture.data[0] = fRawDecodedPicture->data[0];
1523 		rawPicture.data[1] = fRawDecodedPicture->data[1];
1524 		rawPicture.data[2] = fRawDecodedPicture->data[2];
1525 		rawPicture.data[3] = fRawDecodedPicture->data[3];
1526 		rawPicture.linesize[0] = fRawDecodedPicture->linesize[0];
1527 		rawPicture.linesize[1] = fRawDecodedPicture->linesize[1];
1528 		rawPicture.linesize[2] = fRawDecodedPicture->linesize[2];
1529 		rawPicture.linesize[3] = fRawDecodedPicture->linesize[3];
1530 
1531 		avpicture_alloc(&deinterlacedPicture, fContext->pix_fmt, displayWidth,
1532 			displayHeight);
1533 
1534 		if (avpicture_deinterlace(&deinterlacedPicture, &rawPicture,
1535 				fContext->pix_fmt, displayWidth, displayHeight) < 0) {
1536 			TRACE("[v] avpicture_deinterlace() - error\n");
1537 		} else
1538 			useDeinterlacedPicture = true;
1539 	}
1540 
1541 	// Some decoders do not set pix_fmt until they have decoded 1 frame
1542 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
1543 	if (fSwsContext == NULL) {
1544 		fSwsContext = sws_getContext(displayWidth, displayHeight,
1545 			fContext->pix_fmt, displayWidth, displayHeight,
1546 			colorspace_to_pixfmt(fOutputColorSpace),
1547 			SWS_FAST_BILINEAR, NULL, NULL, NULL);
1548 	}
1549 #else
1550 	if (fFormatConversionFunc == NULL) {
1551 		fFormatConversionFunc = resolve_colorspace(fOutputColorSpace,
1552 			fContext->pix_fmt, displayWidth, displayHeight);
1553 	}
1554 #endif
1555 
1556 	fDecodedDataSizeInBytes = fHeader.size_used;
1557 
1558 	if (fDecodedData == NULL) {
1559 		const size_t kOptimalAlignmentForColorConversion = 32;
1560 		posix_memalign(reinterpret_cast<void**>(&fDecodedData),
1561 			kOptimalAlignmentForColorConversion, fDecodedDataSizeInBytes);
1562 	}
1563 	if (fDecodedData == NULL)
1564 		return B_NO_MEMORY;
1565 
1566 	fPostProcessedDecodedPicture->data[0] = fDecodedData;
1567 	fPostProcessedDecodedPicture->linesize[0]
1568 		= fHeader.u.raw_video.bytes_per_row;
1569 
1570 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
1571 	if (fSwsContext != NULL) {
1572 #else
1573 	if (fFormatConversionFunc != NULL) {
1574 #endif
1575 		if (useDeinterlacedPicture) {
1576 			AVFrame deinterlacedFrame;
1577 			deinterlacedFrame.data[0] = deinterlacedPicture.data[0];
1578 			deinterlacedFrame.data[1] = deinterlacedPicture.data[1];
1579 			deinterlacedFrame.data[2] = deinterlacedPicture.data[2];
1580 			deinterlacedFrame.data[3] = deinterlacedPicture.data[3];
1581 			deinterlacedFrame.linesize[0]
1582 				= deinterlacedPicture.linesize[0];
1583 			deinterlacedFrame.linesize[1]
1584 				= deinterlacedPicture.linesize[1];
1585 			deinterlacedFrame.linesize[2]
1586 				= deinterlacedPicture.linesize[2];
1587 			deinterlacedFrame.linesize[3]
1588 				= deinterlacedPicture.linesize[3];
1589 
1590 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
1591 			sws_scale(fSwsContext, deinterlacedFrame.data,
1592 				deinterlacedFrame.linesize, 0, displayHeight,
1593 				fPostProcessedDecodedPicture->data,
1594 				fPostProcessedDecodedPicture->linesize);
1595 #else
1596 			(*fFormatConversionFunc)(&deinterlacedFrame,
1597 				fPostProcessedDecodedPicture, displayWidth, displayHeight);
1598 #endif
1599 		} else {
1600 #if USE_SWS_FOR_COLOR_SPACE_CONVERSION
1601 			sws_scale(fSwsContext, fRawDecodedPicture->data,
1602 				fRawDecodedPicture->linesize, 0, displayHeight,
1603 				fPostProcessedDecodedPicture->data,
1604 				fPostProcessedDecodedPicture->linesize);
1605 #else
1606 			(*fFormatConversionFunc)(fRawDecodedPicture,
1607 				fPostProcessedDecodedPicture, displayWidth, displayHeight);
1608 #endif
1609 		}
1610 	}
1611 
1612 	if (fRawDecodedPicture->interlaced_frame)
1613 		avpicture_free(&deinterlacedPicture);
1614 
1615 	return B_OK;
1616 }
1617