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/. */
|
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
|
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"
|
2017-02-08 01:27:56 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
2014-07-03 06:43:17 +04:00
|
|
|
#include "prsystem.h"
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2017-02-08 01:27:56 +03:00
|
|
|
namespace mozilla {
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2014-11-13 07:50:26 +03:00
|
|
|
StaticMutex FFmpegDataDecoder<LIBAV_VER>::sMonitor;
|
2014-03-21 10:35:15 +04:00
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::FFmpegDataDecoder(FFmpegLibWrapper* aLib,
|
|
|
|
TaskQueue* aTaskQueue,
|
|
|
|
AVCodecID aCodecID)
|
2016-01-20 12:22:43 +03:00
|
|
|
: mLib(aLib)
|
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)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
FFMPEG_LOG("FFmpeg init successful.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<ShutdownPromise>
|
2015-09-21 06:54:29 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Shutdown()
|
|
|
|
{
|
|
|
|
if (mTaskQueue) {
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<FFmpegDataDecoder<LIBAV_VER>> self = this;
|
|
|
|
return InvokeAsync(mTaskQueue, __func__, [self, this]() {
|
|
|
|
ProcessShutdown();
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
});
|
2016-05-09 18:23:48 +03:00
|
|
|
}
|
2017-01-26 15:56:46 +03:00
|
|
|
ProcessShutdown();
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
2016-05-09 18:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaDataDecoder::DecodePromise>
|
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Decode(MediaRawData* aSample)
|
2016-05-09 18:23:48 +03:00
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
return InvokeAsync<MediaRawData*>(mTaskQueue, this, __func__,
|
|
|
|
&FFmpegDataDecoder::ProcessDecode, aSample);
|
2016-05-09 18:23:48 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaDataDecoder::FlushPromise>
|
2014-07-03 06:43:13 +04:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Flush()
|
2014-03-21 10:35:15 +04:00
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
return InvokeAsync(mTaskQueue, this, __func__,
|
|
|
|
&FFmpegDataDecoder<LIBAV_VER>::ProcessFlush);
|
2014-03-21 10:35:15 +04:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaDataDecoder::DecodePromise>
|
2015-09-21 06:54:29 +03:00
|
|
|
FFmpegDataDecoder<LIBAV_VER>::Drain()
|
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
return InvokeAsync(mTaskQueue, this, __func__,
|
|
|
|
&FFmpegDataDecoder<LIBAV_VER>::ProcessDrain);
|
2015-09-21 06:54:29 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaDataDecoder::FlushPromise>
|
2015-09-21 06:54:29 +03:00
|
|
|
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
|
|
|
}
|
2017-01-26 15:56:46 +03:00
|
|
|
return FlushPromise::CreateAndResolve(true, __func__);
|
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
|
2016-06-11 13:04:06 +03:00
|
|
|
mLib->av_freep(&mFrame);
|
2014-07-25 06:20:22 +04:00
|
|
|
#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
|
2016-06-11 13:04:06 +03:00
|
|
|
mLib->av_freep(&mFrame);
|
|
|
|
mFrame = mLib->avcodec_alloc_frame();
|
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
|