xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp (revision 1aa97652e09a9df14ca7f63b32c29514282aaa69)
1 /*
2  * Copyright 2009-2010, Stephan Amßus <superstippi@gmx.de>
3  * Copyright 2018, Dario Casalinuovo
4  * All rights reserved. Distributed under the terms of the MIT license.
5  */
6 
7 
8 #include "AVCodecEncoder.h"
9 
10 #include <new>
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <Application.h>
16 #include <Roster.h>
17 
18 extern "C" {
19 	#include "rational.h"
20 }
21 
22 #include "EncoderTable.h"
23 #include "gfx_util.h"
24 
25 
26 #undef TRACE
27 //#define TRACE_AV_CODEC_ENCODER
28 #ifdef TRACE_AV_CODEC_ENCODER
29 #	define TRACE	printf
30 #	define TRACE_IO(a...)
31 #else
32 #	define TRACE(a...)
33 #	define TRACE_IO(a...)
34 #endif
35 
36 
37 static const size_t kDefaultChunkBufferSize = 2 * 1024 * 1024;
38 
39 
40 AVCodecEncoder::AVCodecEncoder(uint32 codecID, int bitRateScale)
41 	:
42 	Encoder(),
43 	fBitRateScale(bitRateScale),
44 	fCodecID((CodecID)codecID),
45 	fCodec(NULL),
46 	fCodecContext(NULL),
47 	fCodecInitStatus(CODEC_INIT_NEEDED),
48 	fFrame(av_frame_alloc()),
49 	fSwsContext(NULL),
50 	fFramesWritten(0)
51 {
52 	TRACE("AVCodecEncoder::AVCodecEncoder()\n");
53 	_Init();
54 }
55 
56 
57 void
58 AVCodecEncoder::_Init()
59 {
60 	fChunkBuffer = new(std::nothrow) uint8[kDefaultChunkBufferSize];
61 	if (fCodecID > 0) {
62 		fCodec = avcodec_find_encoder(fCodecID);
63 		TRACE("  found AVCodec for %u: %p\n", fCodecID, fCodec);
64 	}
65 
66 	memset(&fInputFormat, 0, sizeof(media_format));
67 
68 	fAudioFifo = av_fifo_alloc(0);
69 
70 	fDstFrame.data[0] = NULL;
71 	fDstFrame.data[1] = NULL;
72 	fDstFrame.data[2] = NULL;
73 	fDstFrame.data[3] = NULL;
74 
75 	fDstFrame.linesize[0] = 0;
76 	fDstFrame.linesize[1] = 0;
77 	fDstFrame.linesize[2] = 0;
78 	fDstFrame.linesize[3] = 0;
79 
80 	// Initial parameters, so we know if the user changed them
81 	fEncodeParameters.avg_field_size = 0;
82 	fEncodeParameters.max_field_size = 0;
83 	fEncodeParameters.quality = 1.0f;
84 }
85 
86 
87 AVCodecEncoder::~AVCodecEncoder()
88 {
89 	TRACE("AVCodecEncoder::~AVCodecEncoder()\n");
90 
91 	if (fSwsContext != NULL)
92 		sws_freeContext(fSwsContext);
93 
94 	av_fifo_free(fAudioFifo);
95 
96 	avpicture_free(&fDstFrame);
97 	// NOTE: Do not use avpicture_free() on fSrcFrame!! We fill the picture
98 	// data on the fly with the media buffer data passed to Encode().
99 
100 	if (fFrame != NULL) {
101 		fFrame->data[0] = NULL;
102 		fFrame->data[1] = NULL;
103 		fFrame->data[2] = NULL;
104 		fFrame->data[3] = NULL;
105 
106 		fFrame->linesize[0] = 0;
107 		fFrame->linesize[1] = 0;
108 		fFrame->linesize[2] = 0;
109 		fFrame->linesize[3] = 0;
110 		av_frame_free(&fFrame);
111 	}
112 
113 	if (fCodecContext != NULL) {
114 		avcodec_close(fCodecContext);
115 		avcodec_free_context(&fCodecContext);
116 	}
117 
118 	delete[] fChunkBuffer;
119 }
120 
121 
122 status_t
123 AVCodecEncoder::AcceptedFormat(const media_format* proposedInputFormat,
124 	media_format* _acceptedInputFormat)
125 {
126 	TRACE("AVCodecEncoder::AcceptedFormat(%p, %p)\n", proposedInputFormat,
127 		_acceptedInputFormat);
128 
129 	if (proposedInputFormat == NULL)
130 		return B_BAD_VALUE;
131 
132 	if (_acceptedInputFormat != NULL) {
133 		memcpy(_acceptedInputFormat, proposedInputFormat,
134 			sizeof(media_format));
135 	}
136 
137 	return B_OK;
138 }
139 
140 
141 status_t
142 AVCodecEncoder::SetUp(const media_format* inputFormat)
143 {
144 	TRACE("AVCodecEncoder::SetUp()\n");
145 
146 	if (inputFormat == NULL)
147 		return B_BAD_VALUE;
148 
149 	// Codec IDs for raw-formats may need to be figured out here.
150 	if (fCodec == NULL && fCodecID == AV_CODEC_ID_NONE) {
151 		fCodecID = raw_audio_codec_id_for(*inputFormat);
152 		if (fCodecID != AV_CODEC_ID_NONE)
153 			fCodec = avcodec_find_encoder(fCodecID);
154 	}
155 	if (fCodec == NULL) {
156 		TRACE("  encoder not found!\n");
157 		return B_NO_INIT;
158 	}
159 
160 	fInputFormat = *inputFormat;
161 	fFramesWritten = 0;
162 
163 	return _Setup();
164 }
165 
166 
167 status_t
168 AVCodecEncoder::GetEncodeParameters(encode_parameters* parameters) const
169 {
170 	TRACE("AVCodecEncoder::GetEncodeParameters(%p)\n", parameters);
171 
172 // TODO: Implement maintaining an automatically calculated bit_rate versus
173 // a user specified (via SetEncodeParameters()) bit_rate. At this point, the
174 // fCodecContext->bit_rate may not yet have been specified (_Setup() was never
175 // called yet). So it cannot work like the code below, but in any case, it's
176 // showing how to convert between the values (albeit untested).
177 //	int avgBytesPerSecond = fCodecContext->bit_rate / 8;
178 //	int maxBytesPerSecond = (fCodecContext->bit_rate
179 //		+ fCodecContext->bit_rate_tolerance) / 8;
180 //
181 //	if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
182 //		fEncodeParameters.avg_field_size = (int32)(avgBytesPerSecond
183 //			/ fInputFormat.u.raw_audio.frame_rate);
184 //		fEncodeParameters.max_field_size = (int32)(maxBytesPerSecond
185 //			/ fInputFormat.u.raw_audio.frame_rate);
186 //	} else if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
187 //		fEncodeParameters.avg_field_size = (int32)(avgBytesPerSecond
188 //			/ fInputFormat.u.raw_video.field_rate);
189 //		fEncodeParameters.max_field_size = (int32)(maxBytesPerSecond
190 //			/ fInputFormat.u.raw_video.field_rate);
191 //	}
192 
193 	parameters->quality = fEncodeParameters.quality;
194 
195 	return B_OK;
196 }
197 
198 
199 status_t
200 AVCodecEncoder::SetEncodeParameters(encode_parameters* parameters)
201 {
202 	TRACE("AVCodecEncoder::SetEncodeParameters(%p)\n", parameters);
203 
204 	if (fFramesWritten > 0)
205 		return B_NOT_SUPPORTED;
206 
207 	fEncodeParameters.quality = parameters->quality;
208 	TRACE("  quality: %.5f\n", parameters->quality);
209 	if (fEncodeParameters.quality == 0.0f) {
210 		TRACE("  using default quality (1.0)\n");
211 		fEncodeParameters.quality = 1.0f;
212 	}
213 
214 // TODO: Auto-bit_rate versus user supplied. See above.
215 //	int avgBytesPerSecond = 0;
216 //	int maxBytesPerSecond = 0;
217 //
218 //	if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
219 //		avgBytesPerSecond = (int)(parameters->avg_field_size
220 //			* fInputFormat.u.raw_audio.frame_rate);
221 //		maxBytesPerSecond = (int)(parameters->max_field_size
222 //			* fInputFormat.u.raw_audio.frame_rate);
223 //	} else if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
224 //		avgBytesPerSecond = (int)(parameters->avg_field_size
225 //			* fInputFormat.u.raw_video.field_rate);
226 //		maxBytesPerSecond = (int)(parameters->max_field_size
227 //			* fInputFormat.u.raw_video.field_rate);
228 //	}
229 //
230 //	if (maxBytesPerSecond < avgBytesPerSecond)
231 //		maxBytesPerSecond = avgBytesPerSecond;
232 //
233 //	// Reset these, so we can tell the difference between uninitialized
234 //	// and initialized...
235 //	if (avgBytesPerSecond > 0) {
236 //		fCodecContext->bit_rate = avgBytesPerSecond * 8;
237 //		fCodecContext->bit_rate_tolerance = (maxBytesPerSecond
238 //			- avgBytesPerSecond) * 8;
239 //		fBitRateControlledByUser = true;
240 //	}
241 
242 	return _Setup();
243 }
244 
245 
246 status_t
247 AVCodecEncoder::Encode(const void* buffer, int64 frameCount,
248 	media_encode_info* info)
249 {
250 	TRACE("AVCodecEncoder::Encode(%p, %lld, %p)\n", buffer, frameCount, info);
251 
252 	if (!_OpenCodecIfNeeded())
253 		return B_NO_INIT;
254 
255 	if (fInputFormat.type == B_MEDIA_RAW_AUDIO)
256 		return _EncodeAudio(buffer, frameCount, info);
257 	else if (fInputFormat.type == B_MEDIA_RAW_VIDEO)
258 		return _EncodeVideo(buffer, frameCount, info);
259 	else
260 		return B_NO_INIT;
261 }
262 
263 
264 // #pragma mark -
265 
266 
267 status_t
268 AVCodecEncoder::_Setup()
269 {
270 	TRACE("AVCodecEncoder::_Setup\n");
271 
272 	int rawBitRate;
273 
274 	fCodecContext = avcodec_alloc_context3(fCodec);
275 	if (fCodecContext == NULL)
276 		return B_NO_INIT;
277 
278 	if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
279 		TRACE("  B_MEDIA_RAW_VIDEO\n");
280 
281 		// Check input parameters
282 		AVPixelFormat pixFmt = colorspace_to_pixfmt(
283 			fInputFormat.u.raw_video.display.format);
284 		if (pixFmt == AV_PIX_FMT_NONE) {
285 			TRACE("Invalid input colorspace\n");
286 			return B_BAD_DATA;
287 		}
288 
289 		// frame rate
290 		fCodecContext->time_base = (AVRational){1, (int)fInputFormat.u.raw_video.field_rate};
291 		fCodecContext->framerate = (AVRational){(int)fInputFormat.u.raw_video.field_rate, 1};
292 
293 		// video size
294 		fCodecContext->width = fInputFormat.u.raw_video.display.line_width;
295 		fCodecContext->height = fInputFormat.u.raw_video.display.line_count;
296 		fCodecContext->gop_size = 12;
297 
298 		// TODO: Fix pixel format or setup conversion method...
299 		if (fCodec->pix_fmts != NULL) {
300 			for (int i = 0; fCodec->pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
301 				// Use the last supported pixel format, which we hope is the
302 				// one with the best quality.
303 				fCodecContext->pix_fmt = fCodec->pix_fmts[i];
304 			}
305 		}
306 
307 		// TODO: Setup rate control:
308 //		fCodecContext->rate_emu = 0;
309 //		fCodecContext->rc_eq = NULL;
310 //		fCodecContext->rc_max_rate = 0;
311 //		fCodecContext->rc_min_rate = 0;
312 		// TODO: Try to calculate a good bit rate...
313 		rawBitRate = (int)(fCodecContext->width * fCodecContext->height * 2
314 			* fInputFormat.u.raw_video.field_rate) * 8;
315 
316 		// Pixel aspect ratio
317 		fCodecContext->sample_aspect_ratio.num
318 			= fInputFormat.u.raw_video.pixel_width_aspect;
319 		fCodecContext->sample_aspect_ratio.den
320 			= fInputFormat.u.raw_video.pixel_height_aspect;
321 		if (fCodecContext->sample_aspect_ratio.num == 0
322 			|| fCodecContext->sample_aspect_ratio.den == 0) {
323 			av_reduce(&fCodecContext->sample_aspect_ratio.num,
324 				&fCodecContext->sample_aspect_ratio.den, fCodecContext->width,
325 				fCodecContext->height, 255);
326 		}
327 
328 		// TODO: This should already happen in AcceptFormat()
329 		if (fInputFormat.u.raw_video.display.bytes_per_row == 0) {
330 			fInputFormat.u.raw_video.display.bytes_per_row
331 				= fCodecContext->width * 4;
332 		}
333 
334 		fFrame->pts = 0;
335 
336 		// Allocate space for colorspace converted AVPicture
337 		// TODO: Check allocations...
338 		avpicture_alloc(&fDstFrame, fCodecContext->pix_fmt, fCodecContext->width,
339 			fCodecContext->height);
340 
341 		// Make the frame point to the data in the converted AVPicture
342 		fFrame->data[0] = fDstFrame.data[0];
343 		fFrame->data[1] = fDstFrame.data[1];
344 		fFrame->data[2] = fDstFrame.data[2];
345 		fFrame->data[3] = fDstFrame.data[3];
346 
347 		fFrame->linesize[0] = fDstFrame.linesize[0];
348 		fFrame->linesize[1] = fDstFrame.linesize[1];
349 		fFrame->linesize[2] = fDstFrame.linesize[2];
350 		fFrame->linesize[3] = fDstFrame.linesize[3];
351 
352 		fSwsContext = sws_getContext(fCodecContext->width,
353 			fCodecContext->height, pixFmt,
354 			fCodecContext->width, fCodecContext->height,
355 			fCodecContext->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
356 
357 	} else if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
358 		TRACE("  B_MEDIA_RAW_AUDIO\n");
359 		// frame rate
360 		fCodecContext->sample_rate = (int)fInputFormat.u.raw_audio.frame_rate;
361 		// channels
362 		fCodecContext->channels = fInputFormat.u.raw_audio.channel_count;
363 		// raw bitrate
364 		rawBitRate = fCodecContext->sample_rate * fCodecContext->channels
365 			* (fInputFormat.u.raw_audio.format
366 				& media_raw_audio_format::B_AUDIO_SIZE_MASK) * 8;
367 		// sample format
368 		switch (fInputFormat.u.raw_audio.format) {
369 			case media_raw_audio_format::B_AUDIO_FLOAT:
370 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_FLT;
371 				break;
372 			case media_raw_audio_format::B_AUDIO_DOUBLE:
373 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_DBL;
374 				break;
375 			case media_raw_audio_format::B_AUDIO_INT:
376 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_S32;
377 				break;
378 			case media_raw_audio_format::B_AUDIO_SHORT:
379 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_S16;
380 				break;
381 			case media_raw_audio_format::B_AUDIO_UCHAR:
382 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_U8;
383 				break;
384 
385 			case media_raw_audio_format::B_AUDIO_CHAR:
386 			default:
387 				return B_MEDIA_BAD_FORMAT;
388 				break;
389 		}
390 		if (fInputFormat.u.raw_audio.channel_mask == 0) {
391 			// guess the channel mask...
392 			switch (fInputFormat.u.raw_audio.channel_count) {
393 				default:
394 				case 2:
395 					fCodecContext->channel_layout = AV_CH_LAYOUT_STEREO;
396 					break;
397 				case 1:
398 					fCodecContext->channel_layout = AV_CH_LAYOUT_MONO;
399 					break;
400 				case 3:
401 					fCodecContext->channel_layout = AV_CH_LAYOUT_SURROUND;
402 					break;
403 				case 4:
404 					fCodecContext->channel_layout = AV_CH_LAYOUT_QUAD;
405 					break;
406 				case 5:
407 					fCodecContext->channel_layout = AV_CH_LAYOUT_5POINT0;
408 					break;
409 				case 6:
410 					fCodecContext->channel_layout = AV_CH_LAYOUT_5POINT1;
411 					break;
412 				case 8:
413 					fCodecContext->channel_layout = AV_CH_LAYOUT_7POINT1;
414 					break;
415 				case 10:
416 					fCodecContext->channel_layout = AV_CH_LAYOUT_7POINT1_WIDE;
417 					break;
418 			}
419 		} else {
420 			// The bits match 1:1 for media_multi_channels and FFmpeg defines.
421 			fCodecContext->channel_layout = fInputFormat.u.raw_audio.channel_mask;
422 		}
423 	} else {
424 		TRACE("  UNSUPPORTED MEDIA TYPE!\n");
425 		return B_NOT_SUPPORTED;
426 	}
427 
428 	// TODO: Support letting the user overwrite this via
429 	// SetEncodeParameters(). See comments there...
430 	int wantedBitRate = (int)(rawBitRate / fBitRateScale
431 		* fEncodeParameters.quality);
432 	if (wantedBitRate == 0)
433 		wantedBitRate = (int)(rawBitRate / fBitRateScale);
434 
435 	fCodecContext->bit_rate = wantedBitRate;
436 
437 	if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
438 		// Some audio encoders support certain bitrates only. Use the
439 		// closest match to the wantedBitRate.
440 		const int kBitRates[] = {
441 			32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000,
442 			160000, 192000, 224000, 256000, 320000, 384000, 448000, 512000,
443 			576000, 640000
444 		};
445 		int diff = wantedBitRate;
446 		for (unsigned int i = 0; i < sizeof(kBitRates) / sizeof(int); i++) {
447 			int currentDiff = abs(wantedBitRate - kBitRates[i]);
448 			if (currentDiff < diff) {
449 				fCodecContext->bit_rate = kBitRates[i];
450 				diff = currentDiff;
451 			} else
452 				break;
453 		}
454 	}
455 
456 	TRACE("  rawBitRate: %d, wantedBitRate: %d (%.1f), "
457 		"context bitrate: %d\n", rawBitRate, wantedBitRate,
458 		fEncodeParameters.quality, fCodecContext->bit_rate);
459 
460 	// Add some known fixes from the FFmpeg API example:
461 	if (fCodecContext->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
462 		// Just for testing, we also add B frames */
463 		fCodecContext->max_b_frames = 2;
464 	} else if (fCodecContext->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
465 		// Needed to avoid using macroblocks in which some coeffs overflow.
466 		// This does not happen with normal video, it just happens here as
467 		// the motion of the chroma plane does not match the luma plane.
468 		fCodecContext->mb_decision = 2;
469 	}
470 
471 	// Unfortunately, we may fail later, when we try to open the codec
472 	// for real... but we need to delay this because we still allow
473 	// parameter/quality changes.
474 	return B_OK;
475 }
476 
477 
478 bool
479 AVCodecEncoder::_OpenCodecIfNeeded()
480 {
481 	if (fCodecInitStatus == CODEC_INIT_DONE)
482 		return true;
483 
484 	if (fCodecInitStatus == CODEC_INIT_FAILED)
485 		return false;
486 
487 	fCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
488 
489 	// Some codecs need this to be set before open
490 	fFrame->format = fCodecContext->pix_fmt;
491 	fFrame->width = fCodecContext->width;
492 	fFrame->height = fCodecContext->height;
493 
494 	// Open the codec
495 	int result = avcodec_open2(fCodecContext, fCodec, NULL);
496 	if (result >= 0)
497 		fCodecInitStatus = CODEC_INIT_DONE;
498 	else
499 		fCodecInitStatus = CODEC_INIT_FAILED;
500 
501 	TRACE("  avcodec_open(%p, %p): %d\n", fCodecContext, fCodec, result);
502 
503 	return fCodecInitStatus == CODEC_INIT_DONE;
504 
505 }
506 
507 
508 status_t
509 AVCodecEncoder::_EncodeAudio(const void* _buffer, int64 frameCount,
510 	media_encode_info* info)
511 {
512 	TRACE("AVCodecEncoder::_EncodeAudio(%p, %lld, %p)\n", _buffer, frameCount,
513 		info);
514 
515 	if (fChunkBuffer == NULL)
516 		return B_NO_MEMORY;
517 
518 	status_t ret = B_OK;
519 
520 	const uint8* buffer = reinterpret_cast<const uint8*>(_buffer);
521 
522 	size_t inputSampleSize = fInputFormat.u.raw_audio.format
523 		& media_raw_audio_format::B_AUDIO_SIZE_MASK;
524 	size_t inputFrameSize = inputSampleSize
525 		* fInputFormat.u.raw_audio.channel_count;
526 
527 	size_t bufferSize = frameCount * inputFrameSize;
528 	bufferSize = min_c(bufferSize, kDefaultChunkBufferSize);
529 
530 	if (fCodecContext->frame_size > 1) {
531 		// Encoded audio. Things work differently from raw audio. We need
532 		// the fAudioFifo to pipe data.
533 		if (av_fifo_realloc2(fAudioFifo,
534 				av_fifo_size(fAudioFifo) + bufferSize) < 0) {
535 			TRACE("  av_fifo_realloc2() failed\n");
536             return B_NO_MEMORY;
537         }
538         av_fifo_generic_write(fAudioFifo, const_cast<uint8*>(buffer),
539         	bufferSize, NULL);
540 
541 		int frameBytes = fCodecContext->frame_size * inputFrameSize;
542 		uint8* tempBuffer = new(std::nothrow) uint8[frameBytes];
543 		if (tempBuffer == NULL)
544 			return B_NO_MEMORY;
545 
546 		// Encode as many chunks as can be read from the FIFO.
547 		while (av_fifo_size(fAudioFifo) >= frameBytes) {
548 			av_fifo_generic_read(fAudioFifo, tempBuffer, frameBytes, NULL);
549 
550 			ret = _EncodeAudio(tempBuffer, frameBytes, fCodecContext->frame_size,
551 				info);
552 			if (ret != B_OK)
553 				break;
554 		}
555 
556 		delete[] tempBuffer;
557 	} else {
558 		// Raw audio. The number of bytes returned from avcodec_encode_audio()
559 		// is always the same as the number of input bytes.
560 		return _EncodeAudio(buffer, bufferSize, frameCount,
561 			info);
562 	}
563 
564 	return ret;
565 }
566 
567 
568 status_t
569 AVCodecEncoder::_EncodeAudio(const uint8* buffer, size_t bufferSize,
570 	int64 frameCount, media_encode_info* info)
571 {
572 	status_t ret;
573 
574 	// Encode one audio chunk/frame.
575 	AVPacket packet;
576 	av_init_packet(&packet);
577 	// By leaving these NULL, we let the encoder allocate memory as it needs.
578 	// This way we don't risk iving a too small buffer.
579 	packet.data = NULL;
580 	packet.size = 0;
581 
582 	// We need to wrap our input data into an AVFrame structure.
583 	AVFrame frame;
584 	int gotPacket = 0;
585 
586 	if (buffer) {
587 		av_frame_unref(&frame);
588 
589 		frame.nb_samples = frameCount;
590 
591 		ret = avcodec_fill_audio_frame(&frame, fCodecContext->channels,
592 				fCodecContext->sample_fmt, (const uint8_t *) buffer, bufferSize, 1);
593 
594 		if (ret != 0)
595 			return B_ERROR;
596 
597 		/* Set the presentation time of the frame */
598 		frame.pts = (bigtime_t)(fFramesWritten * 1000000LL
599 			/ fInputFormat.u.raw_audio.frame_rate);
600 		fFramesWritten += frame.nb_samples;
601 
602 		ret = avcodec_encode_audio2(fCodecContext, &packet, &frame, &gotPacket);
603 	} else {
604 		// If called with NULL, ask the encoder to flush any buffers it may
605 		// have pending.
606 		ret = avcodec_encode_audio2(fCodecContext, &packet, NULL, &gotPacket);
607 	}
608 
609 	if (buffer && frame.extended_data != frame.data)
610 		av_freep(&frame.extended_data);
611 
612 	if (ret != 0) {
613 		TRACE("  avcodec_encode_audio() failed: %ld\n", ret);
614 		return B_ERROR;
615 	}
616 
617 	fFramesWritten += frameCount;
618 
619 	if (gotPacket) {
620 		if (fCodecContext->coded_frame) {
621 			// Store information about the coded frame in the context.
622 			fCodecContext->coded_frame->pts = packet.pts;
623 			// TODO: double "!" operator ?
624 			fCodecContext->coded_frame->key_frame = !!(packet.flags & AV_PKT_FLAG_KEY);
625 		}
626 
627 		// Setup media_encode_info, most important is the time stamp.
628 		info->start_time = packet.pts;
629 
630 		if (packet.flags & AV_PKT_FLAG_KEY)
631 			info->flags = B_MEDIA_KEY_FRAME;
632 		else
633 			info->flags = 0;
634 
635 		// We got a packet out of the encoder, write it to the output stream
636 		ret = WriteChunk(packet.data, packet.size, info);
637 		if (ret != B_OK) {
638 			TRACE("  error writing chunk: %s\n", strerror(ret));
639 			av_free_packet(&packet);
640 			return ret;
641 		}
642 	}
643 
644 	av_free_packet(&packet);
645 	return B_OK;
646 }
647 
648 
649 status_t
650 AVCodecEncoder::_EncodeVideo(const void* buffer, int64 frameCount,
651 	media_encode_info* info)
652 {
653 	TRACE_IO("AVCodecEncoder::_EncodeVideo(%p, %lld, %p)\n", buffer, frameCount,
654 		info);
655 
656 	if (fChunkBuffer == NULL)
657 		return B_NO_MEMORY;
658 
659 	status_t ret = B_OK;
660 
661 	AVPacket* pkt = av_packet_alloc();
662 	while (frameCount > 0) {
663 		size_t bpr = fInputFormat.u.raw_video.display.bytes_per_row;
664 		size_t bufferSize = fInputFormat.u.raw_video.display.line_count * bpr;
665 
666 		// We should always get chunky bitmaps, so this code should be safe.
667 		fSrcFrame.data[0] = (uint8_t*)buffer;
668 		fSrcFrame.linesize[0] = bpr;
669 
670 		// Run the pixel format conversion
671 		sws_scale(fSwsContext, fSrcFrame.data, fSrcFrame.linesize, 0,
672 			fInputFormat.u.raw_video.display.line_count, fDstFrame.data,
673 			fDstFrame.linesize);
674 
675 		if (_EncodeVideoFrame(fFrame, pkt, info) == B_OK) {
676 			// Skip to the next frame (but usually, there is only one to encode
677 			// for video).
678 			frameCount--;
679 			fFramesWritten++;
680 			buffer = (const void*)((const uint8*)buffer + bufferSize);
681 		}
682 	}
683 
684 	// TODO: we should pass a NULL AVFrame and enter "draining" mode, then flush buffers
685 	// when we have finished and there is no more data. We cannot do that here, though, since
686 	// 1. It's not efficient
687 	// 2. It's incorrect, since many codecs need the "next" frame to be able to do optimization.
688 	// if we drain the codec, they cannot work with the "next" frame.
689 	//_EncodeVideoFrame(NULL, pkt, info);
690 	//avcodec_flush_buffers(fCodecContext);
691 	av_packet_free(&pkt);
692 	return ret;
693 }
694 
695 
696 status_t
697 AVCodecEncoder::_EncodeVideoFrame(AVFrame* frame, AVPacket* pkt, media_encode_info* info)
698 {
699 	// Encode one video chunk/frame.
700 	int result = avcodec_send_frame(fCodecContext, frame);
701 	if (result < 0) {
702 		TRACE("  avcodec_send_frame() failed: %d\n", result);
703 		return B_ERROR;
704 	}
705 
706 	// Increase the frame pts as in the ffmpeg sample code
707 	if (frame != NULL)
708 		frame->pts++;
709 
710 	while (result == 0) {
711 		result = avcodec_receive_packet(fCodecContext, pkt);
712 		if (result == 0) {
713 			TRACE("  avcodec_receive_packet: received one packet\n");
714 			// Maybe we need to use this PTS to calculate start_time:
715 			if (pkt->pts != AV_NOPTS_VALUE) {
716 				TRACE("  codec frame PTS: %lld (codec time_base: %d/%d)\n",
717 					pkt->pts, fCodecContext->time_base.num,
718 					fCodecContext->time_base.den);
719 			} else {
720 				TRACE("  codec frame PTS: N/A (codec time_base: %d/%d)\n",
721 					fCodecContext->time_base.num, fCodecContext->time_base.den);
722 			}
723 
724 			// Setup media_encode_info, most important is the time stamp.
725 			info->start_time = (bigtime_t)(fFramesWritten * 1000000LL
726 				/ fInputFormat.u.raw_video.field_rate);
727 
728 			info->flags = 0;
729 			if (fCodecContext->coded_frame->key_frame)
730 				info->flags |= B_MEDIA_KEY_FRAME;
731 
732 			// Write the chunk
733 			result = WriteChunk(pkt->data, pkt->size, info);
734 			if (result != B_OK) {
735 				TRACE("  error writing chunk: %s\n", strerror(result));
736 				break;
737 			}
738 		}
739 		av_packet_unref(pkt);
740 	}
741 	if (result == AVERROR(EAGAIN))
742 		return B_OK;
743 
744 	TRACE("   _EncodeVideoFrame(): returning...\n");
745 	return result;
746 }
747 
748