xref: /haiku/src/add-ons/media/plugins/ffmpeg/AVCodecEncoder.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 	BEncoder(),
43 	fBitRateScale(bitRateScale),
44 	fCodecID((CodecID)codecID),
45 	fCodec(NULL),
46 	fCodecContext(avcodec_alloc_context3(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 	avcodec_free_context(&fCodecContext);
114 
115 	delete[] fChunkBuffer;
116 }
117 
118 
119 status_t
120 AVCodecEncoder::AcceptedFormat(const media_format* proposedInputFormat,
121 	media_format* _acceptedInputFormat)
122 {
123 	TRACE("AVCodecEncoder::AcceptedFormat(%p, %p)\n", proposedInputFormat,
124 		_acceptedInputFormat);
125 
126 	if (proposedInputFormat == NULL)
127 		return B_BAD_VALUE;
128 
129 	if (_acceptedInputFormat != NULL) {
130 		memcpy(_acceptedInputFormat, proposedInputFormat,
131 			sizeof(media_format));
132 	}
133 
134 	return B_OK;
135 }
136 
137 
138 status_t
139 AVCodecEncoder::SetUp(const media_format* inputFormat)
140 {
141 	TRACE("AVCodecEncoder::SetUp()\n");
142 
143 	if (fCodecContext == NULL)
144 		return B_NO_INIT;
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 	if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
275 		TRACE("  B_MEDIA_RAW_VIDEO\n");
276 		// frame rate
277 		fCodecContext->time_base = (AVRational){1, (int)fInputFormat.u.raw_video.field_rate};
278 		fCodecContext->framerate = (AVRational){(int)fInputFormat.u.raw_video.field_rate, 1};
279 
280 		// video size
281 		fCodecContext->width = fInputFormat.u.raw_video.display.line_width;
282 		fCodecContext->height = fInputFormat.u.raw_video.display.line_count;
283 		fCodecContext->gop_size = 12;
284 
285 		// TODO: Fix pixel format or setup conversion method...
286 		if (fCodec->pix_fmts != NULL) {
287 			for (int i = 0; fCodec->pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
288 				// Use the last supported pixel format, which we hope is the
289 				// one with the best quality.
290 				fCodecContext->pix_fmt = fCodec->pix_fmts[i];
291 			}
292 		}
293 
294 		// TODO: Setup rate control:
295 //		fCodecContext->rate_emu = 0;
296 //		fCodecContext->rc_eq = NULL;
297 //		fCodecContext->rc_max_rate = 0;
298 //		fCodecContext->rc_min_rate = 0;
299 		// TODO: Try to calculate a good bit rate...
300 		rawBitRate = (int)(fCodecContext->width * fCodecContext->height * 2
301 			* fInputFormat.u.raw_video.field_rate) * 8;
302 
303 		// Pixel aspect ratio
304 		fCodecContext->sample_aspect_ratio.num
305 			= fInputFormat.u.raw_video.pixel_width_aspect;
306 		fCodecContext->sample_aspect_ratio.den
307 			= fInputFormat.u.raw_video.pixel_height_aspect;
308 		if (fCodecContext->sample_aspect_ratio.num == 0
309 			|| fCodecContext->sample_aspect_ratio.den == 0) {
310 			av_reduce(&fCodecContext->sample_aspect_ratio.num,
311 				&fCodecContext->sample_aspect_ratio.den, fCodecContext->width,
312 				fCodecContext->height, 255);
313 		}
314 
315 		// TODO: This should already happen in AcceptFormat()
316 		if (fInputFormat.u.raw_video.display.bytes_per_row == 0) {
317 			fInputFormat.u.raw_video.display.bytes_per_row
318 				= fCodecContext->width * 4;
319 		}
320 
321 		fFrame->pts = 0;
322 
323 		// Allocate space for colorspace converted AVPicture
324 		// TODO: Check allocations...
325 		avpicture_alloc(&fDstFrame, fCodecContext->pix_fmt, fCodecContext->width,
326 			fCodecContext->height);
327 
328 		// Make the frame point to the data in the converted AVPicture
329 		fFrame->data[0] = fDstFrame.data[0];
330 		fFrame->data[1] = fDstFrame.data[1];
331 		fFrame->data[2] = fDstFrame.data[2];
332 		fFrame->data[3] = fDstFrame.data[3];
333 
334 		fFrame->linesize[0] = fDstFrame.linesize[0];
335 		fFrame->linesize[1] = fDstFrame.linesize[1];
336 		fFrame->linesize[2] = fDstFrame.linesize[2];
337 		fFrame->linesize[3] = fDstFrame.linesize[3];
338 
339 		fSwsContext = sws_getContext(fCodecContext->width, fCodecContext->height,
340 			colorspace_to_pixfmt(fInputFormat.u.raw_video.display.format),
341 			fCodecContext->width, fCodecContext->height,
342 			fCodecContext->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
343 
344 	} else if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
345 		TRACE("  B_MEDIA_RAW_AUDIO\n");
346 		// frame rate
347 		fCodecContext->sample_rate = (int)fInputFormat.u.raw_audio.frame_rate;
348 		// channels
349 		fCodecContext->channels = fInputFormat.u.raw_audio.channel_count;
350 		// raw bitrate
351 		rawBitRate = fCodecContext->sample_rate * fCodecContext->channels
352 			* (fInputFormat.u.raw_audio.format
353 				& media_raw_audio_format::B_AUDIO_SIZE_MASK) * 8;
354 		// sample format
355 		switch (fInputFormat.u.raw_audio.format) {
356 			case media_raw_audio_format::B_AUDIO_FLOAT:
357 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_FLT;
358 				break;
359 			case media_raw_audio_format::B_AUDIO_DOUBLE:
360 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_DBL;
361 				break;
362 			case media_raw_audio_format::B_AUDIO_INT:
363 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_S32;
364 				break;
365 			case media_raw_audio_format::B_AUDIO_SHORT:
366 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_S16;
367 				break;
368 			case media_raw_audio_format::B_AUDIO_UCHAR:
369 				fCodecContext->sample_fmt = AV_SAMPLE_FMT_U8;
370 				break;
371 
372 			case media_raw_audio_format::B_AUDIO_CHAR:
373 			default:
374 				return B_MEDIA_BAD_FORMAT;
375 				break;
376 		}
377 		if (fInputFormat.u.raw_audio.channel_mask == 0) {
378 			// guess the channel mask...
379 			switch (fInputFormat.u.raw_audio.channel_count) {
380 				default:
381 				case 2:
382 					fCodecContext->channel_layout = AV_CH_LAYOUT_STEREO;
383 					break;
384 				case 1:
385 					fCodecContext->channel_layout = AV_CH_LAYOUT_MONO;
386 					break;
387 				case 3:
388 					fCodecContext->channel_layout = AV_CH_LAYOUT_SURROUND;
389 					break;
390 				case 4:
391 					fCodecContext->channel_layout = AV_CH_LAYOUT_QUAD;
392 					break;
393 				case 5:
394 					fCodecContext->channel_layout = AV_CH_LAYOUT_5POINT0;
395 					break;
396 				case 6:
397 					fCodecContext->channel_layout = AV_CH_LAYOUT_5POINT1;
398 					break;
399 				case 8:
400 					fCodecContext->channel_layout = AV_CH_LAYOUT_7POINT1;
401 					break;
402 				case 10:
403 					fCodecContext->channel_layout = AV_CH_LAYOUT_7POINT1_WIDE;
404 					break;
405 			}
406 		} else {
407 			// The bits match 1:1 for media_multi_channels and FFmpeg defines.
408 			fCodecContext->channel_layout = fInputFormat.u.raw_audio.channel_mask;
409 		}
410 	} else {
411 		TRACE("  UNSUPPORTED MEDIA TYPE!\n");
412 		return B_NOT_SUPPORTED;
413 	}
414 
415 	// TODO: Support letting the user overwrite this via
416 	// SetEncodeParameters(). See comments there...
417 	int wantedBitRate = (int)(rawBitRate / fBitRateScale
418 		* fEncodeParameters.quality);
419 	if (wantedBitRate == 0)
420 		wantedBitRate = (int)(rawBitRate / fBitRateScale);
421 
422 	fCodecContext->bit_rate = wantedBitRate;
423 
424 	if (fInputFormat.type == B_MEDIA_RAW_AUDIO) {
425 		// Some audio encoders support certain bitrates only. Use the
426 		// closest match to the wantedBitRate.
427 		const int kBitRates[] = {
428 			32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000,
429 			160000, 192000, 224000, 256000, 320000, 384000, 448000, 512000,
430 			576000, 640000
431 		};
432 		int diff = wantedBitRate;
433 		for (unsigned int i = 0; i < sizeof(kBitRates) / sizeof(int); i++) {
434 			int currentDiff = abs(wantedBitRate - kBitRates[i]);
435 			if (currentDiff < diff) {
436 				fCodecContext->bit_rate = kBitRates[i];
437 				diff = currentDiff;
438 			} else
439 				break;
440 		}
441 	}
442 
443 	TRACE("  rawBitRate: %d, wantedBitRate: %d (%.1f), "
444 		"context bitrate: %d\n", rawBitRate, wantedBitRate,
445 		fEncodeParameters.quality, fCodecContext->bit_rate);
446 
447 	// Add some known fixes from the FFmpeg API example:
448 	if (fCodecContext->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
449 		// Just for testing, we also add B frames */
450 		fCodecContext->max_b_frames = 2;
451 	} else if (fCodecContext->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
452 		// Needed to avoid using macroblocks in which some coeffs overflow.
453 		// This does not happen with normal video, it just happens here as
454 		// the motion of the chroma plane does not match the luma plane.
455 		fCodecContext->mb_decision = 2;
456 	}
457 
458 	// Unfortunately, we may fail later, when we try to open the codec
459 	// for real... but we need to delay this because we still allow
460 	// parameter/quality changes.
461 	return B_OK;
462 }
463 
464 
465 bool
466 AVCodecEncoder::_OpenCodecIfNeeded()
467 {
468 	if (fCodecInitStatus == CODEC_INIT_DONE)
469 		return true;
470 
471 	if (fCodecInitStatus == CODEC_INIT_FAILED)
472 		return false;
473 
474 	fCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
475 
476 	// Some codecs need this to be set before open
477 	fFrame->format = fCodecContext->pix_fmt;
478 	fFrame->width = fCodecContext->width;
479 	fFrame->height = fCodecContext->height;
480 
481 	// Open the codec
482 	int result = avcodec_open2(fCodecContext, fCodec, NULL);
483 	if (result >= 0)
484 		fCodecInitStatus = CODEC_INIT_DONE;
485 	else
486 		fCodecInitStatus = CODEC_INIT_FAILED;
487 
488 	TRACE("  avcodec_open(%p, %p): %d\n", fCodecContext, fCodec, result);
489 
490 	return fCodecInitStatus == CODEC_INIT_DONE;
491 
492 }
493 
494 
495 status_t
496 AVCodecEncoder::_EncodeAudio(const void* _buffer, int64 frameCount,
497 	media_encode_info* info)
498 {
499 	TRACE("AVCodecEncoder::_EncodeAudio(%p, %lld, %p)\n", _buffer, frameCount,
500 		info);
501 
502 	if (fChunkBuffer == NULL)
503 		return B_NO_MEMORY;
504 
505 	status_t ret = B_OK;
506 
507 	const uint8* buffer = reinterpret_cast<const uint8*>(_buffer);
508 
509 	size_t inputSampleSize = fInputFormat.u.raw_audio.format
510 		& media_raw_audio_format::B_AUDIO_SIZE_MASK;
511 	size_t inputFrameSize = inputSampleSize
512 		* fInputFormat.u.raw_audio.channel_count;
513 
514 	size_t bufferSize = frameCount * inputFrameSize;
515 	bufferSize = min_c(bufferSize, kDefaultChunkBufferSize);
516 
517 	if (fCodecContext->frame_size > 1) {
518 		// Encoded audio. Things work differently from raw audio. We need
519 		// the fAudioFifo to pipe data.
520 		if (av_fifo_realloc2(fAudioFifo,
521 				av_fifo_size(fAudioFifo) + bufferSize) < 0) {
522 			TRACE("  av_fifo_realloc2() failed\n");
523             return B_NO_MEMORY;
524         }
525         av_fifo_generic_write(fAudioFifo, const_cast<uint8*>(buffer),
526         	bufferSize, NULL);
527 
528 		int frameBytes = fCodecContext->frame_size * inputFrameSize;
529 		uint8* tempBuffer = new(std::nothrow) uint8[frameBytes];
530 		if (tempBuffer == NULL)
531 			return B_NO_MEMORY;
532 
533 		// Encode as many chunks as can be read from the FIFO.
534 		while (av_fifo_size(fAudioFifo) >= frameBytes) {
535 			av_fifo_generic_read(fAudioFifo, tempBuffer, frameBytes, NULL);
536 
537 			ret = _EncodeAudio(tempBuffer, frameBytes, fCodecContext->frame_size,
538 				info);
539 			if (ret != B_OK)
540 				break;
541 		}
542 
543 		delete[] tempBuffer;
544 	} else {
545 		// Raw audio. The number of bytes returned from avcodec_encode_audio()
546 		// is always the same as the number of input bytes.
547 		return _EncodeAudio(buffer, bufferSize, frameCount,
548 			info);
549 	}
550 
551 	return ret;
552 }
553 
554 
555 status_t
556 AVCodecEncoder::_EncodeAudio(const uint8* buffer, size_t bufferSize,
557 	int64 frameCount, media_encode_info* info)
558 {
559 	status_t ret;
560 
561 	// Encode one audio chunk/frame.
562 	AVPacket packet;
563 	av_init_packet(&packet);
564 	// By leaving these NULL, we let the encoder allocate memory as it needs.
565 	// This way we don't risk iving a too small buffer.
566 	packet.data = NULL;
567 	packet.size = 0;
568 
569 	// We need to wrap our input data into an AVFrame structure.
570 	AVFrame frame;
571 	int gotPacket = 0;
572 
573 	if (buffer) {
574 		av_frame_unref(&frame);
575 
576 		frame.nb_samples = frameCount;
577 
578 		ret = avcodec_fill_audio_frame(&frame, fCodecContext->channels,
579 				fCodecContext->sample_fmt, (const uint8_t *) buffer, bufferSize, 1);
580 
581 		if (ret != 0)
582 			return B_ERROR;
583 
584 		/* Set the presentation time of the frame */
585 		frame.pts = (bigtime_t)(fFramesWritten * 1000000LL
586 			/ fInputFormat.u.raw_audio.frame_rate);
587 		fFramesWritten += frame.nb_samples;
588 
589 		ret = avcodec_encode_audio2(fCodecContext, &packet, &frame, &gotPacket);
590 	} else {
591 		// If called with NULL, ask the encoder to flush any buffers it may
592 		// have pending.
593 		ret = avcodec_encode_audio2(fCodecContext, &packet, NULL, &gotPacket);
594 	}
595 
596 	if (buffer && frame.extended_data != frame.data)
597 		av_freep(&frame.extended_data);
598 
599 	if (ret != 0) {
600 		TRACE("  avcodec_encode_audio() failed: %ld\n", ret);
601 		return B_ERROR;
602 	}
603 
604 	fFramesWritten += frameCount;
605 
606 	if (gotPacket) {
607 		if (fCodecContext->coded_frame) {
608 			// Store information about the coded frame in the context.
609 			fCodecContext->coded_frame->pts = packet.pts;
610 			// TODO: double "!" operator ?
611 			fCodecContext->coded_frame->key_frame = !!(packet.flags & AV_PKT_FLAG_KEY);
612 		}
613 
614 		// Setup media_encode_info, most important is the time stamp.
615 		info->start_time = packet.pts;
616 
617 		if (packet.flags & AV_PKT_FLAG_KEY)
618 			info->flags = B_MEDIA_KEY_FRAME;
619 		else
620 			info->flags = 0;
621 
622 		// We got a packet out of the encoder, write it to the output stream
623 		ret = WriteChunk(packet.data, packet.size, info);
624 		if (ret != B_OK) {
625 			TRACE("  error writing chunk: %s\n", strerror(ret));
626 			av_free_packet(&packet);
627 			return ret;
628 		}
629 	}
630 
631 	av_free_packet(&packet);
632 	return B_OK;
633 }
634 
635 
636 status_t
637 AVCodecEncoder::_EncodeVideo(const void* buffer, int64 frameCount,
638 	media_encode_info* info)
639 {
640 	TRACE_IO("AVCodecEncoder::_EncodeVideo(%p, %lld, %p)\n", buffer, frameCount,
641 		info);
642 
643 	if (fChunkBuffer == NULL)
644 		return B_NO_MEMORY;
645 
646 	status_t ret = B_OK;
647 
648 	AVPacket* pkt = av_packet_alloc();
649 	while (frameCount > 0) {
650 		size_t bpr = fInputFormat.u.raw_video.display.bytes_per_row;
651 		size_t bufferSize = fInputFormat.u.raw_video.display.line_count * bpr;
652 
653 		// We should always get chunky bitmaps, so this code should be safe.
654 		fSrcFrame.data[0] = (uint8_t*)buffer;
655 		fSrcFrame.linesize[0] = bpr;
656 
657 		// Run the pixel format conversion
658 		sws_scale(fSwsContext, fSrcFrame.data, fSrcFrame.linesize, 0,
659 			fInputFormat.u.raw_video.display.line_count, fDstFrame.data,
660 			fDstFrame.linesize);
661 
662 		if (_EncodeVideoFrame(fFrame, pkt, info) == B_OK) {
663 			// Skip to the next frame (but usually, there is only one to encode
664 			// for video).
665 			frameCount--;
666 			fFramesWritten++;
667 			buffer = (const void*)((const uint8*)buffer + bufferSize);
668 		}
669 	}
670 
671 	// TODO: we should pass a NULL AVFrame and enter "draining" mode, then flush buffers
672 	// when we have finished and there is no more data. We cannot do that here, though, since
673 	// 1. It's not efficient
674 	// 2. It's incorrect, since many codecs need the "next" frame to be able to do optimization.
675 	// if we drain the codec, they cannot work with the "next" frame.
676 	//_EncodeVideoFrame(NULL, pkt, info);
677 	//avcodec_flush_buffers(fCodecContext);
678 	av_packet_free(&pkt);
679 	return ret;
680 }
681 
682 
683 status_t
684 AVCodecEncoder::_EncodeVideoFrame(AVFrame* frame, AVPacket* pkt, media_encode_info* info)
685 {
686 	// Encode one video chunk/frame.
687 	int result = avcodec_send_frame(fCodecContext, frame);
688 	if (result < 0) {
689 		TRACE("  avcodec_send_frame() failed: %d\n", result);
690 		return B_ERROR;
691 	}
692 
693 	// Increase the frame pts as in the ffmpeg sample code
694 	if (frame != NULL)
695 		frame->pts++;
696 
697 	while (result == 0) {
698 		result = avcodec_receive_packet(fCodecContext, pkt);
699 		if (result == 0) {
700 			TRACE("  avcodec_receive_packet: received one packet\n");
701 			// Maybe we need to use this PTS to calculate start_time:
702 			if (pkt->pts != AV_NOPTS_VALUE) {
703 				TRACE("  codec frame PTS: %lld (codec time_base: %d/%d)\n",
704 					pkt->pts, fCodecContext->time_base.num,
705 					fCodecContext->time_base.den);
706 			} else {
707 				TRACE("  codec frame PTS: N/A (codec time_base: %d/%d)\n",
708 					fCodecContext->time_base.num, fCodecContext->time_base.den);
709 			}
710 
711 			// Setup media_encode_info, most important is the time stamp.
712 			info->start_time = (bigtime_t)(fFramesWritten * 1000000LL
713 				/ fInputFormat.u.raw_video.field_rate);
714 
715 			info->flags = 0;
716 			if (fCodecContext->coded_frame->key_frame)
717 				info->flags |= B_MEDIA_KEY_FRAME;
718 
719 			// Write the chunk
720 			result = WriteChunk(pkt->data, pkt->size, info);
721 			if (result != B_OK) {
722 				TRACE("  error writing chunk: %s\n", strerror(result));
723 				break;
724 			}
725 		}
726 		av_packet_unref(pkt);
727 	}
728 	if (result == AVERROR(EAGAIN))
729 		return B_OK;
730 
731 	TRACE("   _EncodeVideoFrame(): returning...\n");
732 	return result;
733 }
734 
735