Bug 1187118 part 3 - [WIP] Added GIFDataDecoder implementation of MediaDataDecoder; draft
authorKaku Kuo <kaku@mozilla.com>
Thu, 23 Feb 2017 11:29:28 +0800
changeset 488567 4d506d9c2c7c08285b4628e307be26950f987abc
parent 488566 e1acfc5fe5391f46cc19b91d85309aadc195fb0b
child 488568 6cdb98f04f518bdc45eeda6b675ecfe536d81c02
push id46579
push userbmo:kaku@mozilla.com
push dateThu, 23 Feb 2017 10:26:35 +0000
bugs1187118
milestone54.0a1
Bug 1187118 part 3 - [WIP] Added GIFDataDecoder implementation of MediaDataDecoder; MozReview-Commit-ID: 7JOvy3S3qBi
dom/media/platforms/agnostic/GIFDataDecoder.cpp
dom/media/platforms/agnostic/GIFDataDecoder.h
dom/media/platforms/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/agnostic/GIFDataDecoder.cpp
@@ -0,0 +1,118 @@
+/* -*- 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/. */
+
+#include <algorithm>
+
+#include "GIFDataDecoder.h"
+
+//#include "gfx2DGlue.h"
+//#include "imgFrame.h"
+//#include "mozilla/PodOperations.h"
+#include "mozilla/layers/SharedRGBImage.h"
+//#include "nsError.h"
+//#include "TimeUnits.h"
+
+
+#undef LOG
+#define LOG(arg, ...) MOZ_LOG(sPDMLog, mozilla::LogLevel::Debug, ("GIFDataDecoder(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
+
+namespace mozilla {
+
+using namespace gfx;
+using namespace layers;
+
+GIFDataDecoder::GIFDataDecoder(const CreateDecoderParams& aParams)
+  : mImageContainer(aParams.mImageContainer)
+  , mTaskQueue(aParams.mTaskQueue)
+  , mInfo(aParams.VideoConfig())
+{
+}
+
+GIFDataDecoder::~GIFDataDecoder()
+{
+}
+
+RefPtr<MediaDataDecoder::InitPromise>
+GIFDataDecoder::Init()
+{
+  return InitPromise::CreateAndResolve(TrackInfo::kVideoTrack, __func__);
+}
+
+RefPtr<MediaDataDecoder::DecodePromise>
+GIFDataDecoder::Decode(MediaRawData* aSample)
+{
+  return InvokeAsync<MediaRawData*>(mTaskQueue, this, __func__,
+                                    &GIFDataDecoder::ProcessDecode, aSample);
+}
+
+RefPtr<MediaDataDecoder::DecodePromise>
+GIFDataDecoder::ProcessDecode(MediaRawData* aSample)
+{
+  MOZ_ASSERT(aSample->mExtraData->Length() == 8,
+    "Something is wrong about the size of mExtraData");
+
+  gfx::IntSize frameSize = *(reinterpret_cast<gfx::IntSize*>(aSample->mExtraData->Elements()));
+  RefPtr<Image> image = CreateSharedRGBImage(mImageContainer, frameSize, gfxImageFormat::A8R8G8B8);
+  RefPtr<SharedRGBImage> sharedRGBImage = static_cast<SharedRGBImage*>(image.get());
+  memcpy(sharedRGBImage->GetBuffer(), aSample->Data(), aSample->Size());
+  const IntRect picture(0, 0, frameSize.width, frameSize.height);
+
+  RefPtr<VideoData> v = VideoData::CreateFromImage(mInfo,
+                                                   aSample->mOffset,
+                                                   aSample->mTime,
+                                                   aSample->mDuration,
+                                                   image,
+                                                   aSample->mKeyframe,
+                                                   aSample->mTimecode,
+                                                   picture);
+  if (!v) {
+    LOG("Cannot allocate VideoData.");
+    return DecodePromise::CreateAndReject(MediaResult(NS_ERROR_OUT_OF_MEMORY, __func__), __func__);
+  }
+
+  return DecodePromise::CreateAndResolve(DecodedData{ v }, __func__);
+}
+
+RefPtr<MediaDataDecoder::DecodePromise>
+GIFDataDecoder::Drain()
+{
+  return InvokeAsync(mTaskQueue, __func__, [] {
+    return DecodePromise::CreateAndResolve(DecodedData(), __func__);
+  });
+}
+
+RefPtr<MediaDataDecoder::FlushPromise>
+GIFDataDecoder::Flush()
+{
+  return InvokeAsync(mTaskQueue, __func__, []() {
+    return FlushPromise::CreateAndResolve(true, __func__);
+  });
+}
+
+RefPtr<ShutdownPromise>
+GIFDataDecoder::Shutdown()
+{
+  RefPtr<GIFDataDecoder> self = this;
+  return InvokeAsync(mTaskQueue, __func__, [self]() {
+    return ShutdownPromise::CreateAndResolve(true, __func__);
+  });
+}
+
+const char*
+GIFDataDecoder::GetDescriptionName() const
+{
+  return "GIF image decoder";
+}
+
+/* static */
+bool
+GIFDataDecoder::IsGIF(const nsACString& aMimeType)
+{
+  return aMimeType.EqualsLiteral("image/gif");
+}
+
+} // namespace mozilla
+#undef LOG
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/agnostic/GIFDataDecoder.h
@@ -0,0 +1,40 @@
+/* -*- 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/. */
+#if !defined(GIFDataDecoder_h_)
+#define GIFDataDecoder_h_
+
+#include <stdint.h>
+
+#include "PlatformDecoderModule.h"
+
+namespace mozilla {
+
+class GIFDataDecoder : public MediaDataDecoder
+{
+public:
+  explicit GIFDataDecoder(const CreateDecoderParams& aParams);
+  RefPtr<InitPromise> Init() override;
+  RefPtr<DecodePromise> Decode(MediaRawData* aSample) override;
+  RefPtr<DecodePromise> Drain() override;
+  RefPtr<FlushPromise> Flush() override;
+  RefPtr<ShutdownPromise> Shutdown() override;
+  const char* GetDescriptionName() const override;
+
+  // Return true if mimetype is a GIF file
+  static bool IsGIF(const nsACString& aMimeType);
+
+private:
+  ~GIFDataDecoder();
+  RefPtr<DecodePromise> ProcessDecode(MediaRawData* aSample);
+
+  const RefPtr<layers::ImageContainer> mImageContainer;
+  const RefPtr<TaskQueue> mTaskQueue;
+  const VideoInfo& mInfo;
+};
+
+} // namespace mozilla
+
+#endif
--- a/dom/media/platforms/moz.build
+++ b/dom/media/platforms/moz.build
@@ -1,32 +1,34 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 EXPORTS += [
     'agnostic/AgnosticDecoderModule.h',
+    'agnostic/GIFDataDecoder.h',
     'agnostic/OpusDecoder.h',
     'agnostic/TheoraDecoder.h',
     'agnostic/VorbisDecoder.h',
     'agnostic/VPXDecoder.h',
     'DurationMap.h',
     'MediaTelemetryConstants.h',
     'PDMFactory.h',
     'PlatformDecoderModule.h',
     'wrappers/H264Converter.h',
     'wrappers/MediaDataDecoderProxy.h'
 
 ]
 
 UNIFIED_SOURCES += [
     'agnostic/AgnosticDecoderModule.cpp',
     'agnostic/BlankDecoderModule.cpp',
+    'agnostic/GIFDataDecoder.cpp',
     'agnostic/OpusDecoder.cpp',
     'agnostic/TheoraDecoder.cpp',
     'agnostic/VorbisDecoder.cpp',
     'agnostic/VPXDecoder.cpp',
     'agnostic/WAVDecoder.cpp',
     'PDMFactory.cpp',
     'wrappers/H264Converter.cpp',
     'wrappers/MediaDataDecoderProxy.cpp'