2014-03-21 10:35:15 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-05-05 06:54:44 +03:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
|
2014-03-21 10:35:15 +04:00
|
|
|
#include <string.h>
|
2015-11-12 07:12:12 +03:00
|
|
|
#ifdef __GNUC__
|
2014-03-21 10:35:15 +04:00
|
|
|
#include <unistd.h>
|
2015-11-12 07:12:12 +03:00
|
|
|
#endif
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2014-07-03 06:43:13 +04:00
|
|
|
#include "FFmpegLog.h"
|
2014-03-21 10:35:15 +04:00
|
|
|
#include "FFmpegDataDecoder.h"
|
2014-07-03 06:43:17 +04:00
|
|
|
#include "prsystem.h"
|
2014-03-21 10:35:15 +04:00
|
|
|
|
|
|
|
namespace mozilla
|
|
|
|
{
|
|
|
|
|
2014-11-13 07:50:26 +03:00
|
|
|
StaticMutex FFmpegDataDecoder<LIBAV_VER>::sMonitor;
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2016-01-20 12:22:43 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::FFmpegDataDecoder(FFmpegLibWrapper* aLib,
|
2016-05-11 06:30:57 +03:00
|
|
|
TaskQueue* aTaskQueue,
|
2016-01-20 12:22:43 +03:00
|
|
|
MediaDataDecoderCallback* aCallback,
|
|
|
|
AVCodecID aCodecID)
|
|
|
|
: mLib(aLib)
|
2015-09-21 06:54:29 +03:00
|
|
|
, mCallback(aCallback)
|
2014-07-25 06:20:22 +04:00
|
|
|
, mCodecContext(nullptr)
|
|
|
|
, mFrame(NULL)
|
2014-12-23 06:36:09 +03:00
|
|
|
, mExtraData(nullptr)
|
2014-07-25 06:20:22 +04:00
|
|
|
, mCodecID(aCodecID)
|
2016-05-09 18:23:48 +03:00
|
|
|
, mTaskQueue(aTaskQueue)
|
2016-05-11 06:30:55 +03:00
|
|
|
, mIsFlushing(false)
|
2014-03-21 10:35:15 +04:00
|
|
|
{
|
2016-01-20 12:22:43 +03:00
|
|
|
MOZ_ASSERT(aLib);
|
2014-03-21 10:35:15 +04:00
|
|
|
MOZ_COUNT_CTOR(FFmpegDataDecoder);
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:43:13 +04:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::~FFmpegDataDecoder()
|
|
|
|
{
|
2014-03-21 10:35:15 +04:00
|
|
|
MOZ_COUNT_DTOR(FFmpegDataDecoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-08-11 06:50:07 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::InitDecoder()
|
2014-03-21 10:35:15 +04:00
|
|
|
{
|
|
|
|
FFMPEG_LOG("Initialising FFmpeg decoder.");
|
|
|
|
|
2016-01-20 12:22:43 +03:00
|
|
|
AVCodec* codec = FindAVCodec(mLib, mCodecID);
|
2014-03-21 10:35:15 +04:00
|
|
|
if (!codec) {
|
|
|
|
NS_WARNING("Couldn't find ffmpeg decoder");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:55:18 +03:00
|
|
|
StaticMutexAutoLock mon(sMonitor);
|
|
|
|
|
2016-01-20 12:22:43 +03:00
|
|
|
if (!(mCodecContext = mLib->avcodec_alloc_context3(codec))) {
|
2014-03-21 10:35:15 +04:00
|
|
|
NS_WARNING("Couldn't init ffmpeg context");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-07-23 07:08:27 +04:00
|
|
|
mCodecContext->opaque = this;
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2016-01-06 13:05:59 +03:00
|
|
|
InitCodecContext();
|
2014-07-03 06:43:17 +04:00
|
|
|
|
2014-12-23 06:36:09 +03:00
|
|
|
if (mExtraData) {
|
|
|
|
mCodecContext->extradata_size = mExtraData->Length();
|
2015-04-09 14:14:56 +03:00
|
|
|
// FFmpeg may use SIMD instructions to access the data which reads the
|
|
|
|
// data in 32 bytes block. Must ensure we have enough data to read.
|
|
|
|
mExtraData->AppendElements(FF_INPUT_BUFFER_PADDING_SIZE);
|
2014-12-23 06:36:09 +03:00
|
|
|
mCodecContext->extradata = mExtraData->Elements();
|
|
|
|
} else {
|
|
|
|
mCodecContext->extradata_size = 0;
|
2014-07-11 10:43:59 +04:00
|
|
|
}
|
2014-06-20 02:59:00 +04:00
|
|
|
|
2014-07-28 08:10:29 +04:00
|
|
|
if (codec->capabilities & CODEC_CAP_DR1) {
|
|
|
|
mCodecContext->flags |= CODEC_FLAG_EMU_EDGE;
|
|
|
|
}
|
|
|
|
|
2016-01-20 12:22:43 +03:00
|
|
|
if (mLib->avcodec_open2(mCodecContext, codec, nullptr) < 0) {
|
2014-03-21 10:35:15 +04:00
|
|
|
NS_WARNING("Couldn't initialise ffmpeg decoder");
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->avcodec_close(mCodecContext);
|
|
|
|
mLib->av_freep(&mCodecContext);
|
2014-03-21 10:35:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-07-23 07:08:27 +04:00
|
|
|
if (mCodecContext->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
mCodecContext->sample_fmt != AV_SAMPLE_FMT_FLT &&
|
2014-08-15 10:25:06 +04:00
|
|
|
mCodecContext->sample_fmt != AV_SAMPLE_FMT_FLTP &&
|
|
|
|
mCodecContext->sample_fmt != AV_SAMPLE_FMT_S16 &&
|
|
|
|
mCodecContext->sample_fmt != AV_SAMPLE_FMT_S16P) {
|
|
|
|
NS_WARNING("FFmpeg audio decoder outputs unsupported audio format.");
|
2014-03-21 10:35:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
FFMPEG_LOG("FFmpeg init successful.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:54:29 +03:00
|
|
|
nsresult
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Shutdown()
|
|
|
|
{
|
|
|
|
if (mTaskQueue) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod(this, &FFmpegDataDecoder<LIBAV_VER>::ProcessShutdown);
|
2015-09-21 06:54:29 +03:00
|
|
|
mTaskQueue->Dispatch(runnable.forget());
|
|
|
|
} else {
|
|
|
|
ProcessShutdown();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:23:48 +03:00
|
|
|
void
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::ProcessDecode(MediaRawData* aSample)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
2016-05-11 06:30:55 +03:00
|
|
|
if (mIsFlushing) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-09 18:23:48 +03:00
|
|
|
switch (DoDecode(aSample)) {
|
|
|
|
case DecodeResult::DECODE_ERROR:
|
2016-05-30 19:24:00 +03:00
|
|
|
mCallback->Error(MediaDataDecoderError::DECODE_ERROR);
|
|
|
|
break;
|
|
|
|
case DecodeResult::FATAL_ERROR:
|
|
|
|
mCallback->Error(MediaDataDecoderError::FATAL_ERROR);
|
2016-05-09 18:23:48 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (mTaskQueue->IsEmpty()) {
|
|
|
|
mCallback->InputExhausted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
|
|
|
|
{
|
|
|
|
mTaskQueue->Dispatch(NewRunnableMethod<RefPtr<MediaRawData>>(
|
|
|
|
this, &FFmpegDataDecoder::ProcessDecode, aSample));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-21 10:35:15 +04:00
|
|
|
nsresult
|
2014-07-03 06:43:13 +04:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Flush()
|
2014-03-21 10:35:15 +04:00
|
|
|
{
|
2015-09-21 06:54:29 +03:00
|
|
|
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
|
2016-05-11 06:30:55 +03:00
|
|
|
mIsFlushing = true;
|
2015-09-21 06:54:29 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod(this, &FFmpegDataDecoder<LIBAV_VER>::ProcessFlush);
|
2016-05-05 06:54:44 +03:00
|
|
|
SyncRunnable::DispatchToThread(mTaskQueue, runnable);
|
2016-05-11 06:30:55 +03:00
|
|
|
mIsFlushing = false;
|
2014-03-21 10:35:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-09-21 06:54:29 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Drain()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod(this, &FFmpegDataDecoder<LIBAV_VER>::ProcessDrain);
|
2015-09-21 06:54:29 +03:00
|
|
|
mTaskQueue->Dispatch(runnable.forget());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::ProcessFlush()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
if (mCodecContext) {
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->avcodec_flush_buffers(mCodecContext);
|
2015-09-21 06:54:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::ProcessShutdown()
|
2014-03-21 10:35:15 +04:00
|
|
|
{
|
2014-11-13 07:50:26 +03:00
|
|
|
StaticMutexAutoLock mon(sMonitor);
|
|
|
|
|
2016-01-20 12:22:43 +03:00
|
|
|
if (mCodecContext) {
|
|
|
|
mLib->avcodec_close(mCodecContext);
|
|
|
|
mLib->av_freep(&mCodecContext);
|
2014-07-25 06:20:22 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR >= 55
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->av_frame_free(&mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
#elif LIBAVCODEC_VERSION_MAJOR == 54
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->avcodec_free_frame(&mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
#else
|
|
|
|
delete mFrame;
|
|
|
|
mFrame = nullptr;
|
|
|
|
#endif
|
2014-03-21 10:35:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 06:20:22 +04:00
|
|
|
AVFrame*
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::PrepareFrame()
|
|
|
|
{
|
2015-09-21 06:54:29 +03:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
2014-07-25 06:20:22 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR >= 55
|
|
|
|
if (mFrame) {
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->av_frame_unref(mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
} else {
|
2016-01-20 12:22:43 +03:00
|
|
|
mFrame = mLib->av_frame_alloc();
|
2014-07-25 06:20:22 +04:00
|
|
|
}
|
|
|
|
#elif LIBAVCODEC_VERSION_MAJOR == 54
|
|
|
|
if (mFrame) {
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->avcodec_get_frame_defaults(mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
} else {
|
2016-01-20 12:22:43 +03:00
|
|
|
mFrame = mLib->avcodec_alloc_frame();
|
2014-07-25 06:20:22 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
delete mFrame;
|
|
|
|
mFrame = new AVFrame;
|
2016-01-20 12:22:43 +03:00
|
|
|
mLib->avcodec_get_frame_defaults(mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
#endif
|
|
|
|
return mFrame;
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:55:18 +03:00
|
|
|
/* static */ AVCodec*
|
2016-01-20 12:22:43 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::FindAVCodec(FFmpegLibWrapper* aLib,
|
|
|
|
AVCodecID aCodec)
|
2015-10-06 14:55:18 +03:00
|
|
|
{
|
2016-01-20 12:22:43 +03:00
|
|
|
return aLib->avcodec_find_decoder(aCodec);
|
2015-10-06 14:55:18 +03:00
|
|
|
}
|
2016-01-20 12:22:43 +03:00
|
|
|
|
2014-03-21 10:35:15 +04:00
|
|
|
} // namespace mozilla
|